编写 Sumif 公式时出现 VBA 语法错误



我尝试在VBA中编写以下代码行:

Range("S5")="=SUMIF(L2:L793,">=0")"

但是,当我尝试运行代码时出现语法错误。错误突出显示为"("。希望有人可以帮助解决这个问题!

正如评论中提到的,问题在于"符号的转义。通常,这是将任何Excel公式"翻译"为VBA的简单方法:

  1. 在Excel中编写公式,因此它可以正常工作。
  2. 选择包含公式的单元格。
  3. 运行以下代码:

Public Sub PrintMeUsefulFormula()
    Dim strFormula  As String
    Dim strParenth  As String
    strParenth = """"
    strFormula = Selection.Formula
    strFormula = Replace(strFormula, """", """""")
    strFormula = strParenth & strFormula & strParenth
    Debug.Print strFormula
End Sub

  1. 查看即时窗口 (Ctrl+ G(。

最新更新