This relates to another question I recently posted, which @Stavros Jon kindly helped me with.
I am trying to create a named range based on criteria in column B and column C. I want to create a range if column B contains the word "OSI" and column C contains the word "Language".
I have tried editing my previous code, but I cannot get the syntax right and get an object error with the counter line.
Sub another()
'Create Ranges:
Dim featuresRng As Range
Dim rng As Range
Dim sht As Worksheet
Dim counter As Long
Dim cell As Range
Set sht = ThisWorkbook.Worksheets("Features")
Set featuresRng = sht.Range(sht.Range("C1"), sht.Range("C" & sht.Rows.Count).End(xlUp)) 'dynamically set the range of features
Set featuresRng2 = sht.Range(sht.Range("B1"), sht.Range("B" & sht.Rows.Count).End(xlUp))
counter = 0 'this counter will help us avoid Union(Nothing, some range), which would give an error
For Each cell In featuresRng 'loop through the range of features
If featuresRng.cell.Value = "Language" And featuresRng2.cell.Value = "OSI" Then
counter = counter + 1
If counter = 1 Then
Set rng = sht.Range(cell.Offset(0, 1), cell.Offset(0, 3))
Else
Set rng = Union(rng, sht.Range(cell.Offset(0, 1), cell.Offset(0, 3))) 'build the range
End If
End If
Next cell
Debug.Print rng.Address
ThisWorkbook.Names.Add "OSILAng", rng
End Sub
How can I edit my code to include these two criteria?
Also, sometimes my text in column B will contain words in other cells, like "Filter" and "Filter and Search", I am also looking to make my range from the EXACT text in column C cells, not just 'contains this text.
Thanks in advance!
Try this