Apply Conditional Formatting via VBA Fails Expected end of Statement

detriez

Board Regular
Joined
Sep 13, 2011
Messages
193
Office Version
  1. 365
Platform
  1. Windows
With help from the forum I got this conditional formatting work.
Code:
=IFERROR(VLOOKUP(H2,'Standard Values'!D:G,4,0)="MQL",FALSE)

I need to move it to VBA but, it fails at "MQL" with Compile error: Expected End of statement

VBA Code:
    Range("H2:H250000").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(H2<>"""",=IFERROR(VLOOKUP(H2,'Standard Values'!D:G,4,0)="MQL",FALSE)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = vbBlue
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
In VBA if a formula has quotation marks inside it you need to add extra ones

"" Will need to be """"
" Will need to be ""

So "MQL" needs to be ""MQL""
 
Upvote 0
Try
VBA Code:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND(H2<>"""",IF(VLOOKUP(H2,'Standard Values'!D:G,4,0)=""MQL"",TRUE,FALSE))"
 
Upvote 0
Solution
Thanks all
Removing the first = and adding double quotes did the trick.

I Appreciate the help
 
Upvote 0
You are welcome
And thank you for the feedback
Be happy & safe
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

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