Arfi
Arfi

Reputation: 31

Excel If statement in VBA

I have huge issue with VBA. I want to write this statement:=IF(C5<>0;(D5/C5);" ")

at VBA code. But this:

Cells(y_2, 5) = "=IF(C" & y_2 & "<>0;(D" & y_2 & "/C" & y_2 & ");"" "")"

enter image description here

doesn't work and I have no idea what is wrong. y_2 is declared as Integer.

Help. Someone.

Upvotes: 3

Views: 56

Answers (2)

Zsmaster
Zsmaster

Reputation: 1559

Change the ";" character to "," character

Cells(y_2, 5) = "=IF(C" & y_2 & "<>0,(D" & y_2 & "/C" & y_2 & "),"" "")"

Upvotes: 2

Xabier
Xabier

Reputation: 7735

How about changing your code from:

Cells(y_2,5) = "IF(C" & y_2 & "<>0;"(D" & y_2 & "/C" & y_2 & ");"" "")"

To this:

Sheets("YourSheetName").Cells(y_2,5).FormulaR1C1 = "=IF(RC[-2]<>0;RC[-1]/RC[-2];"""")"
'remember to change the name of your sheet above

Upvotes: 0

Related Questions