Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting rows in excel based on the start of postcodes

Tags:

excel

vba

So I have a data file that includes the postcodes of supporters and I need to highlight the rows that contain a number of them. At the moment I have the following code;

Sub ScotTest()

Dim iLastRow As Long
Dim i As Long
    iLastRow = Cells(Rows.Count, "F").End(xlUp).Row
    For i = iLastRow To 1 Step -1
        If Cells(i, "H").Text Like "TD" & "*" Then
            Rows(i).Interior.ColorIndex = 4
        End If
    Next i
End Sub

Whilst this highlights the correct rows with all postcodes beginning with TD I now need to also highlight postcodes beginning with KY and KA.

Thanks

like image 887
Robbo2020 Avatar asked Jan 25 '26 04:01

Robbo2020


1 Answers

Simply adding an OR to your IF should do the trick :)

Sub ScotTest()
    Dim iLastRow As Long
    Dim i As Long
    iLastRow = Cells(Rows.Count, "F").End(xlUp).Row
    For i = iLastRow To 1 Step -1
        If Cells(i, "H").Text Like "TD" & "*" OR _
           Cells(i, "H").Text Like "KY" & "*" OR _
           Cells(i, "H").Text Like "KA" & "*" Then
            Rows(i).Interior.ColorIndex = 4
        End If
    Next i
End Sub
like image 176
hammythepig Avatar answered Jan 27 '26 21:01

hammythepig