2015-10-13 20 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插入時,我必須使用逗號「,」。