将= IFERROR函数添加到公式中
我正在尝试将= IFERROR函数添加到Excel公式中。 在活动单元格中的原始公式示例: =A1/B3
,示例新公式: =IFERROR(A1/B3;0)
。 但是,它会导致运行时错误。 消息框中显示的公式似乎是正确的。
我已经testing了与variables添加括号到公式相同的代码: First_part = "=(" and Last_Part = ")"
,它工作正常。 我也用IF函数使用variablestesting了相同的代码: 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创build公式时,您需要使用逗号而不是分号。 所以,改变这个:
Last_Part = ";0)"
对此:
Last_Part = ",0)"
在昨天花了几个小时之后,我在发布问题5秒后find了解决scheme。
解:
Last_Part =“,0”
我用分号“;” 作为Excel中的分隔符,但是当从VBA插入时,我不得不使用逗号“,”。