r/visualbasic • • Sep 02 '20

Error while writing VB in excel

I am new to Visual basic and trying to write a vba formula to use as a conditional formatting in excel. This is the snipit that is giving me fits.

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _

"=AND(G2<>"NR", ISBLANK(H2),ISBLANK(I2))"

Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

The above is throwing an syntax error. The "NR" portion is highlighted. When I paste that same AND() formula into a cell it seems to work just fine. What am I doing wrong?

8 Upvotes

2 comments sorted by

6

u/danjimian Sep 02 '20

Because Formula1 is a string, and "NR" is a string within that string, you need to double-up on the double quotes thus:

"=AND(G2<>""NR"", ISBLANK(H2),ISBLANK(I2))"

2

u/rivitingone Sep 02 '20

Ok now that I see it with your explanation it makes complete sense. Wish I had thought of asking here days ago. Thank you so much.