WorksheetFunction SumProduct 嵌套循环中出现错误 1004

问题描述 投票:0回答:1

尝试在循环内调用

WorksheetFunction.SumProduct
时遇到“1004”错误。

我想要实现的目标:连续过滤两个变量,然后创建可见单元格的范围,仅使用 sumproduct 函数计算体积加权平均值。

非常感谢任何帮助!

请在下面找到我的代码:

With wsS
     .AutoFilterMode = False
        With Range("B1:U" & lRow)
                For i = 0 To uBS - 1 'Step -1
                    '.AutoFilter Field:=10, Criteria1:=sArray(i) ', Operator:=xlFilterValues
                    .AutoFilter Field:=10, Criteria1:=result(i)
                        For j = 0 To uBD - 1
                            .AutoFilter Field:=8, Criteria1:=result2(j)
                            endrowTs = Worksheets(10).Cells(Rows.Count, 2).SpecialCells(xlCellTypeLastCell).End(xlUp).Row
                                If Not endrowTs < 2 Then
                                    With Worksheets(10).Range("M2:M" & endrowTs)
                                    Set r1 = .SpecialCells(xlCellTypeVisible)
                                    End With
                                    With Worksheets(10).Range("M2:M" & endrowTs)
                                    Set r2 = .SpecialCells(xlCellTypeVisible)
                                    End With
                                    VolG = WorksheetFunction.SumProduct(r1, r2)
                                Else
                                End If
                            
                        Next j
                Next i
        End With
    .AutoFilterMode = False
End With

已经尝试了很多,但到目前为止没有任何帮助,例如

VolG = Application.WorksheetFunction.SumProduct(.Range("L2:L" & lRow).SpecialCells(xlCellTypeVisible), .Range("M2:M" & lRow).SpecialCells(xlCellTypeVisible))

excel vba for-loop worksheet-function with-statement
1个回答
0
投票

在下面的表格中,您可以看到我们如何在过滤范围上使用

SUMPRODUCT
函数的说明。您可以使用公式或 VBA 代码。公式的可能版本:

=SUMPRODUCT($C$3:$C$9,$D$3:$D$9,SUBTOTAL(3,INDIRECT("B"&ROW($B$3:$B$9))))
=SUMPRODUCT($C$3:$C$9,$D$3:$D$9,SUBTOTAL(3,OFFSET($B$3,ROW($B$3:$B$9)-ROW($B$3),0)))

3 个版本的 VBA 代码(它们都可以工作,但是我没有比较它们在更大范围内的速度)。

Sub SumProductWithFilter1()
       Dim Subtot As Double, V As Variant, r As Long
       With WorksheetFunction
              V = Range("B3:B9").Value
              For r = 1 To UBound(V)
                      V(r, 1) = .Subtotal(3, Range("B3:B9")(r))
              Next r
              Subtot = .SumProduct(Range("C3:C9").Value, _
                       Range("D3:D9").Value, V)
       End With
       If IsNumeric(Subtot) Then MsgBox Subtot
End Sub

Sub SumProductWithFilter2()
       Dim Subtot As Double, V As Variant, c As Long
        V = Range("B3:B9").Value
              For c = 1 To UBound(V)
                      V(c, 1) = 1 + CLng(Range("B3:B9")(c).Rows.Hidden)
              Next c
       With WorksheetFunction
              Subtot = .SumProduct(Range("C3:C9").Value, _
                       Range("D3:D9").Value, V)
       End With
       If IsNumeric(Subtot) Then MsgBox Subtot
End Sub

Sub SumProductWithFilter3()
       Dim Subtot
       Subtot = ActiveSheet.Evaluate("SUMPRODUCT(C3:C9,D3:D9,SUBTOTAL(3,OFFSET(B3,ROW(B3:B9)-ROW(B3),0)))")
       If IsNumeric(Subtot) Then MsgBox Subtot
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.