FormulaR1C1和" = SUMIFS("

时间:2017-12-05 21:44:28

标签: excel-vba sumifs vba excel

我尝试根据业务的BUcodeDivision添加特定位置类型中占用位置的所有单元。

  

注意:
   - 变量 binshelv 已占用是工作表中定义的名称。
   - 变量 Division BUcode 在父代码中定义,该代码调用并将值传递给此过程。

以下是我要输入的代码:

ActiveCell.FormulaR1C1 = _
    "=IF(" & _
        "" & BUcode & "=Overall," & _
        "if(" & _
                "" & Division & "=Overall, " & _
                "SUMIFS(" & _
                        "DataDump[On Hand Qty (Units)]," & _
                        "DataDump[Occupied?], occupied," & _
                        "DataDump[Location Short Description], binshelv" & _
                ")," & _
                "Sumifs(" & _
                        "DataDump[On Hand Qty (Units)]," & _
                        "DataDump[Occupied?], occupied," & _
                        "DataDump[Location Short Description], binshelv," & _
                        "DataDump[Division Code], " & DivCode & "," & _
                ")" & _
        ")," & _
        "SUMIFS(" & _
                "DataDump[On Hand Qty (Units)]," & _
                "DataDump[Business Unit]," & BUcode & "," & _
                "DataDump[Location Short Description], binshelv" & _
        ")" & _

    ")"

为什么我目前正在Error '1004'

1 个答案:

答案 0 :(得分:0)

似乎问题是你的公式不等于有效的字符串。如果将以下代码复制并粘贴到VBA编辑器中,您将看到strFormula不等同于有效字符串。这意味着您在公式中出错。要记住的一件事是,您需要在公式中引用引号,或使用转义字符,以在字符串中使用文字引号。

试着让“strFormula =”等同于一个有效的字符串。

Sub aa()

    Dim strFormula As String
    Dim binshelv, occupied, Division, BUcode As String

    ' **--- Get this line to equate to a valid string**
    strFormula = _
    "=IF(" & _
    "" & BUcode & "=Overall," & _
    "if(" & _
            "" & Division & "=Overall, " & _
            "SUMIFS(" & _
                    "DataDump[On Hand Qty (Units)]," & _
                    "DataDump[Occupied?], occupied," & _
                    "DataDump[Location Short Description], binshelv" & _
            ")," & _
            "Sumifs(" & _
                    "DataDump[On Hand Qty (Units)]," & _
                    "DataDump[Occupied?], occupied," & _
                    "DataDump[Location Short Description], binshelv," & _
                    "DataDump[Division Code], " & DivCode & "," & _
            ")" & _
    ")," & _
    "SUMIFS(" & _
            "DataDump[On Hand Qty (Units)]," & _
            "DataDump[Business Unit]," & BUcode & "," & _
            "DataDump[Location Short Description], binshelv" & _
    ")" & _
    ")"

    ActiveCell.FormulaR1C1 = strFormula
End Sub

使用双引号表示文字引号变为

=SUMIFS(C8:C11,A8:A11,"colACriteria",B8:B11,"ColBCriteria")

"=SUMIFS(C8:C11,A8:A11,""colACriteria"",B8:B11,""ColBCriteria"")"