Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can´t add = in formula with VBA, Excel

Tags:

excel

vba

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.

like image 440
Sukakka Avatar asked Dec 25 '22 17:12

Sukakka


1 Answers

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"

like image 163
Siddharth Rout Avatar answered Jan 31 '23 13:01

Siddharth Rout