0

I'm struggling to write a formula in Excel VBA.

This is my code:

Dim Formula as Range
Dim Number as Integer

Number = 1

Formula.formula = "=MAX(IF(LEFT(B7:B250,1)= & Number &, B7:B250))"

It gives a syntax error.

Community
  • 1
  • 1
ChangeWorld
  • 421
  • 1
  • 6
  • 22
  • 1
    Does this answer your question? [How can I insert variable into formula in VBA](https://stackoverflow.com/questions/42503316/how-can-i-insert-variable-into-formula-in-vba) – BigBen Jan 28 '20 at 13:33
  • 1
    You are missing some quotes: `"=MAX(IF(LEFT(B7:B250,1)=" & Number & ",B7:B250))"`. – BigBen Jan 28 '20 at 13:33

1 Answers1

1

You are missing some quotes:

"=MAX(IF(LEFT(B7:B250,1)=" & Number & ",B7:B250))"

Also, this is an array formula so use .FormulaArray instead of .Formula.

BigBen
  • 46,229
  • 7
  • 24
  • 40