【发布时间】:2015-10-13 16:12:35
【问题描述】:
我正在尝试将 =IFERROR 函数添加到 Excel 公式。活动单元格中的示例原始公式:=A1/B3,示例新公式:=IFERROR(A1/B3;0)。但是它会导致运行时错误。消息框中显示的公式似乎是正确的。
我已经测试了使用变量为公式添加括号的相同代码:First_part = "=(" and Last_Part = ")",效果很好。我还使用变量使用 IF 函数测试了相同的代码:
First_part = "=IF(F1=2;" and Last_Part = ";0)",这也导致了运行时错误。
Sub Adding_IFERROR()
Dim Cell_Content As String
Dim First_Part As String
Dim Last_Part As String
Dim New_Cell_Content As String
First_Part = "=IFERROR("
Last_Part = ";0)"
'Remove the initial "=" sign from original formula
Cell_Content = ActiveCell.Formula
Cell_Content = Right(Cell_Content, Len(Cell_Content) - 1)
'Writing new formula
New_Cell_Content = First_Part & Cell_Content & Last_Part
MsgBox New_Cell_Content, vbOKOnly
ActiveCell.Formula = New_Cell_Content
End Sub
它为什么不起作用有什么明显的原因吗?
【问题讨论】: