VBA Apply Conditional Formatting Formula If Cell Contains...

CravingGod

New Member
Joined
Dec 31, 2014
Messages
7
Novice here. I'm sure it's simple. I have some previously existing code that works fine, but I think I need to add either an "if" statement or an "and" statement to the formula somehow.

I have a report that is generated and the code below provides conditional formatting if H>G starting on row 3. I only need this rule to apply if the corresponding cell in column K contains "Pipe".

Thanks in advance.

Code:
' Conditional format columns G:H where H>G
  
    Range("H3").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$H3>$G3"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 49407
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("G3:H3,G3:H1048576").Select
    Range("H3").Activate
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$H3>$G3"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 49407
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("G1:H2").Select
    Selection.FormatConditions.Delete
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It looks like your existing code added the same condition twice.

Try this...
Code:
[color=green]' Conditional format columns G:H where H>G and K contains "Pipe"[/color]
    
    [color=darkblue]With[/color] Range("G3:H" & Rows.Count)
        .Select
        [color=darkblue]With[/color] .FormatConditions
            .Delete
            [color=darkblue]With[/color] .Add(Type:=xlExpression, Formula1:="=AND($H3>$G3,SEARCH(""Pipe"",$K3))").Interior
                .PatternColorIndex = xlAutomatic
                .Color = 49407
                .TintAndShade = 0
            [color=darkblue]End[/color] [color=darkblue]With[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
 
Last edited:
Upvote 0
This is where Macro Recorder comes in handy. Record yourself doing it manually, then view the VBA code the Recorder creating, and apply it to your code.
So change this part:
Code:
Formula1:="=$H3>$G3"
to
Code:
Formula1:= "=AND($H3>$G3,$K3=""Pipe"")"


Edit: I wasn't quite clear if column K will equal "Pipe" exactly, or may contain "Pipe" along with other text. If so, then go with AlphaFrog's suggestion.
 
Last edited:
Upvote 0
Thanks Joe4. That seemed to work just fine.

AlphaFrog, thank you for the effort you put into correcting my problem. I sure it would have worked, but I wasn't confident enough in my own coding ability make it jive with everything else.

I appreciate it guys.
 
Upvote 0

Forum statistics

Threads
1,214,884
Messages
6,122,082
Members
449,064
Latest member
MattDRT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top