Home » excel » Excel If statement in VBA

Excel If statement in VBA

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

Change the “;” character to “,” character

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

Answer:

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