Excel VBA 1004 错误公式

Excel VBA 1004 Error formulas

我已经在 VBA 工作了大约一个月,我觉得我对这门语言的掌握还不错。不幸的是,这就是为什么这个错误如此令人沮丧。

我正在尝试将公式分配给单元格,但我没有使用英式分隔符(因此使用逗号而不是分号)。我仍然遇到运行时错误。

实际公式的语法很好,因为我已经尝试在 Excel 中使用它,并且它可以满足我的需要,所以我必须假设错误在我的 [=22] 中=]语法,如下:

    With DecileChooser
    Range("A1").CurrentRegion.PasteSpecial

    Range("I1").Value = CurrentPD
    Range("I2").Value = CompanyDomain


    DecileChooser.Range("J2").Formula = "=IF($I > D2, G2, "")"

    Range("J2:J11").FillDown

    Range("K1").FormulaLocal = "=max(J1:J11)"

    Range("K1").Value = DecileValue
End With  

这不是整个子程序,只是操作部分。

加倍双引号:

DecileChooser.Range("J2").Formula = "=IF($I > D2, G2, """")"

dq = Chr(34)
DecileChooser.Range("J2").Formula = "=IF($I > D2, G2," & dq & dq & ")"

这里的问题是代码正在读取公式中的双引号作为公式的结尾,即它读取的是:

DecileChooser.Range("J2").Formula = "=IF($I > D2, G2, "

但是剩下的“)”出现语法问题

您必须使用双引号来表示您没有结束字符串。

With DecileChooser
Range("A1").CurrentRegion.PasteSpecial

Range("I1").Value = CurrentPD
Range("I2").Value = CompanyDomain

DecileChooser.Range("J2").Formula = "=IF($I > D2, G2, """")"

Range("J2:J11").FillDown

Range("K1").FormulaLocal = "=max(J1:J11)"

Range("K1").Value = DecileValue
End With