【问题标题】:Insert Formula with Variable in VBA在 VBA 中插入带变量的公式
【发布时间】:2018-05-11 14:58:51
【问题描述】:

我正在尝试使用 VBA 将变量用于公式。我已经引用了How to pass an integer variable into a vba formula 这个问题,并尝试在我的 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 &")"

谢谢。

【问题讨论】:

  • startCell 为 8 且 8 不是单元格。你是说单元格(startCell, 3)?
  • @Jeeped 不。 startCell 是一个整数,它是一个会改变的变量。它不是指向一个单元格。我不应该把它命名为 startcell。
  • 将数字硬编码到“公式”中是毫无用处的。为什么不判断标准并将结果写入单元格?

标签: excel vba


【解决方案1】:

你只需要在&前后加空格

这就是为什么

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

【讨论】:

  • 如果我稍微改变一下以使我的公式 >0 并且我尝试了这个,将 >end 行更改为 >0 但我收到错误:DDSS_Actual.Cells(headerrow + 3, DDSS_InvQTY).Formula = "=IF(" & Delta_val & " " > " & 0 & ", " & Delta_val & "+" & 确认 & "," & Delta_val2 & "+" & 确认 & ")"
  • 你的问题是什么?
  • 我如何编写公式来将 ">" & endCell & 更改为 ">" 0 ?我试过了,我得到了错误
  • 使它成为 ">0"。你应该更加努力地自己弄清楚这一点。
  • 是的,我应该更加努力。我打算声明 endcell = 0。谢谢
猜你喜欢
  • 2018-01-27
  • 1970-01-01
  • 2017-07-18
  • 1970-01-01
  • 2017-07-23
  • 2017-07-19
相关资源
最近更新 更多