Andreas Hermle
asked on
Searching for a string in data list and determine column using VBA
Dear Experts:
below code highlights the rows where the value in Column 'C' (item codes) starts with the string '03-'.
The item codes starting with the string '03-' are located in just one column but they may be in a different column in another data list.
So could somebody please help me rewrite this code so that ...
... the macro searches for the first occurrence of the string '03-' in the data list and sets that column as a variable replacing the hard coded "C" in line 4
Help is much appreciated.
Thank you very much in advance.
Regards, Andreas
below code highlights the rows where the value in Column 'C' (item codes) starts with the string '03-'.
The item codes starting with the string '03-' are located in just one column but they may be in a different column in another data list.
So could somebody please help me rewrite this code so that ...
... the macro searches for the first occurrence of the string '03-' in the data list and sets that column as a variable replacing the hard coded "C" in line 4
Help is much appreciated.
Thank you very much in advance.
Regards, Andreas
Sub Hightlight_Rows_On_Condition()
Dim row As Range
For Each row In ActiveSheet.UsedRange.Rows
If InStr(1, row.Cells(1, "C"), "03-") Then
row.Interior.ColorIndex = 6
End If
Next row
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In rereading your question I'm not sure that I interpreted it correctly. I assume now that you are always looking for "03-", and you want the column it looks in to be a variable. Is that correct?
I modified your macro so it would look for a cell containing a value that started with 03-. The macro then searched every cell in that column beginning with 03- and colored it.
Sub Hightlight_Rows_On_Condition()
Dim cel As Range, rg As Range, row As Range
Set cel = ActiveSheet.UsedRange.Find("03-*", LookAt:=xlWhole, LookIn:=xlValues, SearchOrder:=xlByRows)
Set rg = Intersect(cel.EntireColumn, ActiveSheet.UsedRange)
For Each row In rg.Rows
If InStr(1, row.Value, "03-")=1 Then
row.Interior.ColorIndex = 6
End If
Next row
End Sub
ASKER
Dear Martin,
thank you very much for your swift and professional help. My question was not unequivocal, I have to admit, actually I was looking for a solution provided by Brad. Although his solution still requires a bit tweaking.
NEVERTHELESS, your approach is very good, It works just fine and I will integrate your approach in another macro I will be running.
Brad: Thank you very much for your professional help. The macro works just fine although it would be great if you could tweak it so that the rows (not the entire row, but just the used range of the rows) where this criterion is met gets shaded, not only the respective cells.
Help is much appreciated. Thank you very much in advance.
Regards, Andreas
thank you very much for your swift and professional help. My question was not unequivocal, I have to admit, actually I was looking for a solution provided by Brad. Although his solution still requires a bit tweaking.
NEVERTHELESS, your approach is very good, It works just fine and I will integrate your approach in another macro I will be running.
Brad: Thank you very much for your professional help. The macro works just fine although it would be great if you could tweak it so that the rows (not the entire row, but just the used range of the rows) where this criterion is met gets shaded, not only the respective cells.
Help is much appreciated. Thank you very much in advance.
Regards, Andreas
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dear both,
since I can use both codes, I have split the points!
Thank you very much for your great, professional and swift help. I really appreciate it.
Good job!
Regards, Andreas
since I can use both codes, I have split the points!
Thank you very much for your great, professional and swift help. I really appreciate it.
Good job!
Regards, Andreas
You're welcome and I'm glad I was able to help.
Marty - MVP 2009 to 2013
Marty - MVP 2009 to 2013