2015-10-13 24 views
2

我试图添加= IFERROR函数到Excel公式。在活动单元格中的原始公式示例:=A1/B3,示例新公式:=IFERROR(A1/B3;0)。但它会导致运行时错误。消息框中显示的公式似乎是正确的。Add = IFERROR函数公式

我已经测试了相同的代码,用变量添加括号给公式: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 

是否有任何明显的原因,为什么它不起作用?

回答

1

使用VBA创建公式时,您需要使用逗号而不是分号。 因此,改变这种:

Last_Part = ";0)" 

这样:

Last_Part = ",0)" 
+0

好抓!我正要将此问题标记为[this]的重复(http://stackoverflow.com/questions/32902128/how-to-apply-iferror-to-all-cells-in-excel/32902671#32902671)当我意识到你会发现它失败的真正原因。 – Jeeped

0

花费数小时昨天之后,我找到了解决办法5秒张贴的问题后。

解决方案:

Last_Part =” 0"

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