Add a cell formula in Excel via vba

雨燕双飞 提交于 2019-12-05 07:11:44

Try using .formula = instead of .value = in your VBA code.

Setting the .value of a cell simply copies in whatever value you specify. In this case, your formula is simply converted to a string value.

Using the .formula property, you are actually specifying the formula that gets used to compute the value, which is what you are looking for.

Can I first suggest a simplification of your formula, from:

=IF(OR(ISNUM(D570)=FALSE;ISNUM(D573)=FALSE);"";IF(C573="Total";D573-D570;""))

...to...

=IF(AND(C573="Total"; ISNUM(D570); ISNUM(D573)); D573-D570; "")

Then, I'd set a cell (the active cell in the example below) to use that formula using the VBA code:

ActiveCell.Formula = "=IF(...)"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!