【问题标题】:Add =IFERROR function to formulas将 =IFERROR 函数添加到公式
【发布时间】: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

它为什么不起作用有什么明显的原因吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    在通过 VBA 创建公式时,您需要使用逗号而不是分号。 所以,改变这个:

    Last_Part = ";0)"
    

    到这里:

    Last_Part = ",0)"
    

    【讨论】:

    • 好收获!我正准备将此问题标记为与this 重复时,我意识到您已经发现了它失败的真正原因。
    【解决方案2】:

    昨天花了几个小时后,我在发布问题 5 秒后找到了解决方案。

    解决办法:

    Last_Part = ",0"

    我使用分号“;”作为excel中的分隔符,但是从VBA插入时我必须使用逗号“,”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多