首页 > 解决方案 > 在 VBA 中插入带变量的公式

问题描述

我正在尝试使用 VBA 将变量用于公式。我已经参考了如何将整数变量传递给 vba 公式这个问题,并尝试在我的 if 公式中实现它。=IF (startCell >0, startcell+confirmed, endcell+confirmed)

但我仍然不确定并遇到错误。

我的代码如下:

headerrow = 7
startCell = 8
endCell = 3
confirmed= 5

DDSS_Actual.Cells(headerrow + 3, 3).Formula = "=IF(" & startCell & ">" & endCell & ","& startCell & "+" &confirmed&", " & endCell & "+" &confirmed &")"

谢谢。

标签: excelvba

解决方案


你只需要在之前和之后放置空格&

这使得它

DDSS_Actual.Cells(headerrow + 3, 3).Formula = "=IF(" & startCell & ">" & endCell & "," & startCell & "+" & confirmed & "," & endCell & "+" & confirmed & ")"

推荐阅读