VBA Apply Formatting Based on 2 Criteria and If Another Cell Contains...

wiseh2014

New Member
Joined
Mar 26, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I'm sure it's simple but cant figure out where I am going wrong. I have some previously existing code that works fine, but I don't know how to modify this to only apply these formatting conditions if another column contains certain values.

I have a report that is generated and the code below provides conditional formatting if the value in D = 0 and value in G>D, but I only want this to happen if the value in B contains any of the following values: EA, LF, VLF, SF or CY.

I have seen people add SEARCH(""Value"',$B2) but I do not know where I would add that and if I can add all those different values into the SEARCH function.
I have tried
"=AND($D2=0,$G2>$D2,SEARCH(""EA","LF","VLF","SF","CY"",$B2))"
And also tried it without the commas between each value.

Any advice would be appreciated.

Thank you!

Range("Table13[Week Ending Units]").Select
Application.CutCopyMode = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($D2=0,$G2>$D2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 10066431
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hello and welcome to forum, @wiseh2014!
Try
VBA Code:
=AND($D2=0, $G2>$D2, OR($B2="EA", $B2="LF", $B2="VLF", $B2="SF", $B2="CY""))
 
Upvote 0
Hello and welcome to forum, @wiseh2014!
Try
VBA Code:
=AND($D2=0, $G2>$D2, OR($B2="EA", $B2="LF", $B2="VLF", $B2="SF", $B2="CY""))
Hi, thank you for getting back to me! I tried your line but still am receiving a compile error. See image below. It seems to be catching on the values that I want the formula to look for when applying the formatting.


1711543098808.png
 
Upvote 0
If you are trying to do this in VBA, the issue is that double-quotes are used as Text qualifiers. So if you want literal double-quotes, you usually have to double-them up.
This can get really confusing. I found the following method much more successful:

Turn on your Macro Recorder and record yourself setting up an example of this rule that you want (your Conditional Formatting using your formula).
Then stop the Macro Recorder, and view the code you just recorded. Then you can copy/paste the formula piece that you need for you code, and that will ensure it is written just the way that VBA requires.
 
Upvote 0
If you are trying to do this in VBA, the issue is that double-quotes are used as Text qualifiers. So if you want literal double-quotes, you usually have to double-them up.
This can get really confusing. I found the following method much more successful:

Turn on your Macro Recorder and record yourself setting up an example of this rule that you want (your Conditional Formatting using your formula).
Then stop the Macro Recorder, and view the code you just recorded. Then you can copy/paste the formula piece that you need for you code, and that will ensure it is written just the way that VBA requires.
Hi, I tried this as well but maybe I am creating the conditional formatting rule wrong? I recorded one where I filter B for those specific values then create a rule for D to highlight when D is 0 and G>D. I did another where I used =AND($B2="EA",$D2=0,$G2>$D2) and tried that changing the EA for each additional value I want it for. However I cannot get either to work.

When one does work, it seems to be highlighting cells in D that are 0 but where G is not greater. Ex - It highlights cell D9 (G=0 here) where I would need it to highlight D10 (G=19 and B=VLF).

1711556537918.png
 
Upvote 0
Your line of code in red should look like this:
VBA Code:
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($D2=0, $G2>$D2, OR($B2=""EA"", $B2=""LF"", $B2=""VLF"", $B2=""SF"", $B2=""CY""))"
Note the double-quote marks around each of the text values for B2, like I mentioned before.
You would get this exact syntax by turning on the Macro Recorder and entering Conditional Formatting rule manually.
 
Upvote 0
Solution
Your line of code in red should look like this:
VBA Code:
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($D2=0, $G2>$D2, OR($B2=""EA"", $B2=""LF"", $B2=""VLF"", $B2=""SF"", $B2=""CY""))"
Note the double-quote marks around each of the text values for B2, like I mentioned before.
You would get this exact syntax by turning on the Macro Recorder and entering Conditional Formatting rule manually.
That worked, thank you so much!! Sorry, I must have misinterpreted that last message about the double quotations.

I really appreciate the support.
 
Upvote 0
You are welcome!
Glad we were able to help.
 
Upvote 0
You are welcome!
Glad we were able to help.
Hey, sorry one last question. I noticed lower down that the formatting still applies when the value in B isnt one that we specified. Would it make a difference if I switched the order of the function to have the OR criteria first? Or maybe replicating the whole chain of rules for each criteria, changing the value for B in each like below?

Range("Table13[Week Ending Units]").Select
Application.CutCopyMode = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($D2=0, $G2>$D2, OR($B2=""EA"")
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 10066431
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

Range("Table13[Week Ending Units]").Select
Application.CutCopyMode = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($D2=0, $G2>$D2, OR($B2=""VLF"")
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 10066431
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
 
Upvote 0
I am really sure what you are asking.
I am also not really sure why you are splitting the same rule. And it doesn't really make sense to have an "OR" with only one condition. There is no point to that.

And this line is incomplete anyway, as you are missing the closing parentheses and double-quote, i.e.
VBA Code:
"=AND($D2=0, $G2>$D2, OR($B2=""VLF"")
should be:
VBA Code:
"=AND($D2=0, $G2>$D2, OR($B2=""VLF""))"
(but like I said, having an "OR" with just one condition is pointless)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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