Sukakka
Sukakka

Reputation: 55

Can´t add = in formula with VBA, Excel

When I try to make a dynamic formula with VBA in excel i get this error message.

This line is just fine:

ActiveCell.Value = "IF(SUM(" & Range("A1:A5").Address & ")*0,1>" & Range("B1").Address & ";" & Range("C1").Address & ";SUM(" & Range("A1:A5").Address & ")*0,1)*-1"

If i add = in front of my IF-statement like this, I get the error.

ActiveCell.Value = "=IF(SUM(" & Range("A1:A5").Address & ")*0,1>" & Range("B1").Address & ";" & Range("C1").Address & ";SUM(" & Range("A1:A5").Address & ")*0,1)*-1"

If i just add the = sign after testing the first code. It runs fine in excel. What am i doing wrong here?

Don't mind the ranges and stuff. They are only placeholders to make the example as similar to my code as possible.

Upvotes: 4

Views: 181

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149295

To enter a formula in a cell you have to use the .Formula property instead of .Value.

Try this

ActiveCell.Formula = "=IF(SUM(" & Range("A1:A5").Address & ")*0,1>" & Range("B1").Address & ";" & Range("C1").Address & ";SUM(" & Range("A1:A5").Address & ")*0,1)*-1"

Upvotes: 10

Related Questions