Home » excel » Excel 2010 VBA Macro Conditional Formatting of row if cell contains text snippet

Excel 2010 VBA Macro Conditional Formatting of row if cell contains text snippet

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m doing conditional formatting in a macro (because I’m constantly applying it along with other formatting repeatedly to a fresh, raw export).

Objective: highlight any row where the text in cell J(n) is “No Activity”

Currently using:

    With Cells
.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=($J1=""No Activity"")"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = 7405514
            .TintAndShade = 0
        End With
        StopIfTrue = False
    End With
End With

…which works great. The above was cleaned up using a google search and a recording that originally gave me:

    Cells.Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=($N1=""No Activity"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent4
    .TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False

So I was feeling all proud and accomplished… but I also want to highlight rows (in a different color) where the cell in Column J (per above) contains “Quote” at any point in the text of the cell.

When I recorded a macro of doing it as conditional formatting, it didn’t really clarify anything for me: (ok, it made it worse)

    Selection.FormatConditions.Add Type:=xlTextString, String:="Quote", _
    TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent1
    .TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False

I’m just not catching how it should change in

Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"

All ideas greatly appreciated!

How to&Answers:

This works in Excel 2010:

With Cells
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=($J1=""No Activity"")"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = 7405514
            .TintAndShade = 0
        End With
        StopIfTrue = False
    End With

    .FormatConditions.Add Type:=xlExpression, Formula1:= _
      "=ISNUMBER(SEARCH(""*quote*"",$J1))"
    With .FormatConditions(.FormatConditions.Count)
        .SetFirstPriority
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = 4405514
            .TintAndShade = 0
        End With
        StopIfTrue = False
    End With
End With

Obviously you’d need to change the Color for the 2nd FormatConditions.Add section.

Edit: Realized you were looking for “Quote” anywhere in the cell, so I’ve updated the code from my original posting.