0

I am tying to replicate an excel formula IF(A2>300,"PASS","FAIL") using Range.Formula in VBA, but it is not working as required.

I am trying to call variables into the formula itself as shown below but it is retuing "end of statement" error. Somebody please help! Thanks

col_label = "A"
Range("B1").Formula = "=IF(" & col_label & 2" > 300, ""PASS"", ""FAIL"")"
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Umar Aftab
  • 147
  • 4
  • 15
  • Possible duplicate of https://stackoverflow.com/questions/45321109/vba-variable-in-formula and https://stackoverflow.com/questions/54400248/variables-in-formula – AJD Sep 02 '19 at 20:03
  • Possible duplicate of [VBA -- variable in .formula](https://stackoverflow.com/questions/45321109/vba-variable-in-formula) – AJD Sep 02 '19 at 20:03

1 Answers1

1

Everything after col_label should be in quotes (double-quoted where necessary):

col_label = "A"
Range("B1").Formula = "=IF(" & col_label & "2 > 300, ""PASS"", ""FAIL"")"

enter image description here

Tom Sharpe
  • 30,727
  • 5
  • 24
  • 37