变量求和求和范围
Variable sumifs sum range
我得到一个
Compile error: Type Mismatch
我的代码出错。代码背后的想法是根据 j
循环有一个可变的总和范围。请不要批评我,因为我对 VBA 还是很陌生。请帮忙。
Sub eiegraanGB()
Application.ScreenUpdating = False
Dim i As Integer
Dim j As Integer
Dim arg1 As Range
Dim arg2 As Range
Dim arg3 As Range
Dim arg4 As Range
Dim arg5 As Range
Dim arg6 As Range
Dim arg7 As Range
Dim arg8 As Range
Dim arg9 As Range
Dim arg10 As Range
Dim arg11 As Range
Dim arg As Integer
Dim k As Range
Worksheets("HR Grn Bedryf").Activate
Set arg2 = Worksheets("Sorted Data").Range("I:I")
Set arg3 = Worksheets("Sorted Data").Range("M:M")
Set arg4 = Worksheets("Sorted Data").Range("O:O")
Set arg5 = Worksheets("Sorted Data").Range("N:N")
Set arg6 = Worksheets("Sorted Data").Range("P:P")
Set arg7 = Worksheets("Sorted Data").Range("J:J")
Set arg8 = Worksheets("Sorted Data").Range("A:A")
Set arg10 = Worksheets("Sorted Data").Range("B:B")
For j = 2 To 7
Set k = arg & j
For i = 184 To 2386
Set arg9 = Cells(i, 1)
Set arg11 = Cells(i, 12)
Cells(i, 1).Select
If ActiveCell.Value = "BVH EIE GRAAN" Or ActiveCell.Value = "AANKOPE EIE REK. GRN" Or ActiveCell.Value = "EVH EIE GRAAN" Or ActiveCell.Value = "VERKOPE EIE GRAAN" Then
Cells(i, j) = Application.WorksheetFunction.SumIfs(k, arg8, arg9, arg10, arg11)
Else
End If
Next i
Next j
Application.ScreenUpdating = True
End Sub
为此您需要使用数组。定义arg
为1到11的数组:
Dim arg(1 To 11) As Range
Set arg(2) = Worksheets("Sorted Data").Range("I:I")
然后你可以这样使用它:
Set k = arg(j)
注意:我建议阅读 How to avoid using Select in Excel VBA 以使您的代码更快更稳定。
我还建议使用 Long
而不是 Integer
Excel 的行数超过 Integer
可以处理的数量,使用 Integer
没有任何好处完全 VBA。
我得到一个
Compile error: Type Mismatch
我的代码出错。代码背后的想法是根据 j
循环有一个可变的总和范围。请不要批评我,因为我对 VBA 还是很陌生。请帮忙。
Sub eiegraanGB()
Application.ScreenUpdating = False
Dim i As Integer
Dim j As Integer
Dim arg1 As Range
Dim arg2 As Range
Dim arg3 As Range
Dim arg4 As Range
Dim arg5 As Range
Dim arg6 As Range
Dim arg7 As Range
Dim arg8 As Range
Dim arg9 As Range
Dim arg10 As Range
Dim arg11 As Range
Dim arg As Integer
Dim k As Range
Worksheets("HR Grn Bedryf").Activate
Set arg2 = Worksheets("Sorted Data").Range("I:I")
Set arg3 = Worksheets("Sorted Data").Range("M:M")
Set arg4 = Worksheets("Sorted Data").Range("O:O")
Set arg5 = Worksheets("Sorted Data").Range("N:N")
Set arg6 = Worksheets("Sorted Data").Range("P:P")
Set arg7 = Worksheets("Sorted Data").Range("J:J")
Set arg8 = Worksheets("Sorted Data").Range("A:A")
Set arg10 = Worksheets("Sorted Data").Range("B:B")
For j = 2 To 7
Set k = arg & j
For i = 184 To 2386
Set arg9 = Cells(i, 1)
Set arg11 = Cells(i, 12)
Cells(i, 1).Select
If ActiveCell.Value = "BVH EIE GRAAN" Or ActiveCell.Value = "AANKOPE EIE REK. GRN" Or ActiveCell.Value = "EVH EIE GRAAN" Or ActiveCell.Value = "VERKOPE EIE GRAAN" Then
Cells(i, j) = Application.WorksheetFunction.SumIfs(k, arg8, arg9, arg10, arg11)
Else
End If
Next i
Next j
Application.ScreenUpdating = True
End Sub
为此您需要使用数组。定义arg
为1到11的数组:
Dim arg(1 To 11) As Range
Set arg(2) = Worksheets("Sorted Data").Range("I:I")
然后你可以这样使用它:
Set k = arg(j)
注意:我建议阅读 How to avoid using Select in Excel VBA 以使您的代码更快更稳定。
我还建议使用 Long
而不是 Integer
Excel 的行数超过 Integer
可以处理的数量,使用 Integer
没有任何好处完全 VBA。