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
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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:

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,493
Office Version
365
Platform
Windows
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:

CravingGod

New Member
Joined
Dec 31, 2014
Messages
7
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.
 

Forum statistics

Threads
1,082,258
Messages
5,364,081
Members
400,778
Latest member
Canadian Sal

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top