2014-10-20 63 views
0

我遇到Excel VBA出現運行時錯誤「1004」的問題。引用一個已經有雙引號的字符串

下面的代碼:

Sub modifyformula() 

Dim formla As String 

formla = Cells(21, 48).Formula 

Cells(21, 48).Formula = "=IF(ISERROR(" & formla & ",," & formla & ")" 

End Sub 

基本上我試圖做的是,如果告訴VBA添加在「= IF(ISERROR(」 ...到現有的公式,我會告訴VBA更新每個包含相似公式的單元格

單元格中的公式爲:= GETPIVOTDATA($ AU $ 6 & $ AU $ 19,Pivottable!$ A $ 3,「mis_month」,AV $ 3,「channel2」 ,$ AU21)

我覺得問題是雙引號在實際公式中是造成e錯誤,但我不知道如何解決它。

在此先感謝那些可以幫助我的人:)。

回答

0

你需要串聯之前削減了從公式「=」:

Sub Tester() 

    Const NEW_FORM As String = "=IF(ISERROR(<f>),,<f>)" 

    Dim f As String, c As Range 

    Set c = Cells(21, 48) 

    If c.HasFormula Then 
     f = Right(c.Formula, Len(c.Formula) - 1) 
     c.Formula = Replace(NEW_FORM, "<f>", f) 
    End If 

End Sub 

注:IFERROR()是有點更容易在這裏使用,這取決於你需要支持什麼樣的Excel版本。