0

I keep receiving a Compile Error: Syntax error on this line of code. I can not figure out why

Range("EB2").Formula "=If(OR(DY2=1, DZ2=1,EA2=1),"Y","N")"

It keeps highlighting the "Y" portion of the code

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

1 Answers1

0

In order to have double quotes preserved in your string literal (which has double quotes around it) you can double the double quotes. You also need an equal sign after .Formula as you are setting the formula property of the range. (One equal for the formula property setting, another equal inside the double quotes for the excel formula once it's placed in the cell. (Thanks @Gary'sStudent)

Range("EB2").Formula = "=If(OR(DY2=1, DZ2=1,EA2=1),""Y"",""N"")"

The first double quote (in the pair) escapes the second double quote. Essentially telling VBA to treat it as a literal double quote instead of the start/end of a string literal.

JNevill
  • 46,980
  • 4
  • 38
  • 63