gpt4 book ai didi

vba - 将 =IFERROR 函数添加到公式

转载 作者:行者123 更新时间:2023-12-04 20:41:14 25 4
gpt4 key购买 nike

我正在尝试将 =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 创建公式时,您需要使用逗号而不是分号。
所以,改变这个:

Last_Part = ";0)"

对此:
Last_Part = ",0)"

关于vba - 将 =IFERROR 函数添加到公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com