VBA SUMIFS - 不起作用,所有输入都返回正确的值

时间:2017-02-07 18:13:11

标签: vba excel-vba sumifs excel

以下代码在我运行时抛出1004运行时错误。调试器在第2部分的上面突出显示SUMIFS功能。

此子目标的目标是使用第1,3和5列定位重复的行作为"主键"然后根据每行的值,按行6-7和10-17的列组合值。因此,SUMIFS。

我很困惑。使用立即窗口我可以为SUMIFS功能的每个部分返回正确的值(ws.Cells(lRow,lCol).Select将选择正确的单元格等)。我的下一个想法是SUMIFS没有正确解释范围,所以我使用特定范围将相同的函数弹出到立即窗口并收到相同的错误。请参阅下面的行以立即进入窗口 - 请注意,目标是在此示例中组合行21:23之间的值。

debug.Print application.WorksheetFunction.SumIfs(range("F21:F23"), range("A9:A30"), range("A21").Value, range("C9:C30"), range("C21").Value, range("E9:E30"), range("E21").Value)

我假设并且100%确定命名范围,。代数和变量正在按预期工作。那就是说,我以前错了。

非常感谢任何帮助。

Private Sub dba_combine_rows()

Const COL_TRIPS = 6
Const COL_EMP_TRIP = 7
Const COL_LN_HC = 10
Const COL_USN_PR = 17
Dim lLastRow As Long

Dim ws As Worksheet
Set ws = DBA

Dim answer As Integer
answer = MsgBox("Are you sure you want to combine rows?", vbYesNo, "Combine Rows")
If answer = vbNo Then
    Exit Sub
End If

'Get the last row
    Dim i As Long
    For i = Range("inputRange" & ws.CodeName).Column To (Range("inputRange" & ws.CodeName).Column + Range("inputRange" & ws.CodeName).Columns.Count - 1)
        If ws.Cells(ws.Rows.Count, i).End(xlUp).Row > lLastRow Then
            lLastRow = ws.Cells(ws.Rows.Count, i).End(xlUp).Row
        End If
    Next i

''Combine, start modify

    'Set aliases for columns A & B & C, used for checking duplicates
    Dim rngA As Range, rngB As Range, rngC As Range
    Set rngA = ws.Range("inputRange" & ws.CodeName).Columns(1)
    Set rngB = ws.Range("inputRange" & ws.CodeName).Columns(3)
    Set rngC = ws.Range("inputRange" & ws.CodeName).Columns(5)

    Dim lRow As Long, lCol As Long, strHolderA As String, lHolderR As Long
    For lRow = ws.Range("inputRange" & ws.CodeName).Row To lLastRow

    'Part 1 - Check for duplicate entity-country
        If Application.CountIfs(rngA, ws.Cells(lRow, rngA.Column), rngB, ws.Cells(lRow, rngB.Column), rngC, ws.Cells(lRow, rngC.Column)) > 1 Then

        strHolderA = (ws.Cells(lRow, rngA.Column).Value & ws.Cells(lRow, rngB.Column).Value & ws.Cells(lRow, rngC.Column).Value)
        lHolderR = lRow

            For lCol = COL_TRIPS To COL_USN_PR
                If lCol = COL_EMP_TRIP Then
                    lCol = COL_LN_HC
                End If
                ws.Cells(lRow, lCol).Value = Application.WorksheetFunction.SumIfs( _
                    ws.Range(Col_Letter(lCol) & lRow & ":" & Col_Letter(lCol) & lLastRow), rngA, ws.Cells(lRow, rngA.Column).Value, rngB, ws.Cells(lRow, rngB.Column).Value, rngC, ws.Cells(lRow, rngC.Column).Value)
            Next lCol

        'Part 2 - Delete similar rows, excluding 1st
            Dim lRow2 As Long
            For lRow2 = ws.Range("inputRange" & ws.CodeName).Row To lLastRow

                If (ws.Cells(lRow2, rngA.Column).Value & ws.Cells(lRow2, rngB.Column).Value & _
                        ws.Cells(lRow2, rngC.Column).Value) = strHolderA And lRow2 <> lHolderR Then
                    Rows(lRow2 & ":" & lRow2).Select
                    Selection.Delete Shift:=xlUp
                    lRow2 = lRow2 - 1
                End If
            Next lRow2

        End If

    Next lRow

End Sub

1 个答案:

答案 0 :(得分:0)

sum_range参数(第一个)必须与条件范围相同,即全部。另外,必须是相同的大小。

"F21:F23"只有3个单元格,而其他"A9:A30"每个计数22个单元格。