引用已经有双引号的字符串

时间:2014-10-20 00:00:01

标签: excel vba excel-vba

我在使用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)

我认为问题是实际公式中的双引号导致错误,但我不知道如何修复它。

提前感谢那些可以帮助我的人:)。

1 个答案:

答案 0 :(得分: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版本。