Question

I want to highlight the row that contains letters starting with "ZZ*" green
Then highlight the row that contains letters starting with "ZV*" yellow
and so on.

I used conditional formatting formula inside VB script, it works with the first condition but then ignores the second condition. If anyone has a different way or can improve my code it'll be great. thank you.

Sub color1()
   With Range("A:Z").FormatConditions
      .Delete
      Range("A2").Activate
      .Add Type:=xlExpression, Formula1:="=ISNUMBER(MATCH(""ZZ*"",1:1,0))"
      .Item(1).Interior.ColorIndex = 41    
   End With
End Sub

The A column has

ZZ3543   
ZV5635   
ZX4635   
ZC3456     
ZV3456   
Was it helpful?

Solution

This one works for me:

Sub color1()
    'change Sheet1 to suit
    With ThisWorkbook.Worksheets("Sheet1").Range("A:Z").FormatConditions
        .Delete
        .Add Type:=xlExpression, Formula1:="=ISNUMBER(MATCH(""ZZ*"",1:1,0))"
        .Item(1).Interior.ColorIndex = 4 'green
        .Add Type:=xlExpression, Formula1:="=ISNUMBER(MATCH(""ZV*"",1:1,0))"
        .Item(2).Interior.ColorIndex = 6 'yellow
    End With
End Sub

enter image description here

OTHER TIPS

Here's something you can start with:

Sub Color1()
    For i = 1 To 5
        If Left(Sheet1.Cells(i, 1), 2) = "ZZ" Then Sheet1.Rows(i).Interior.ColorIndex = 41
    Next
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top