[英]Excel VBA - if loop with dynamic filter range
I have the following table: 我有下表:
Filter Notional Type Total
A 5000 A _____
B 1000 A
2500 B
3600 C
7500 C
8000 B
4500 C
700 C
And the following code in order to calculate the filtered sum of notional values: 下面的代码是为了计算过滤后的名义值之和:
Sub filtersum()
Dim filterA, filterB as Variant
filterA = Cells (2, 1)
filterB = Cells (3, 1)
Dim i, n as Integer
k = Cells.Find("Type").Offset(1, 0).Row
n = Cells.Find("Type").End(xlDown).Row
Dim sum as Double
sum = 0
For i = k To n
If Cells(i, 3) = filterA Or Cells(i, 3) = filterB Then
j = Cells(i, 3)
sum = j + sum
End If
Next i
Cells.(2, 4).Value = sum 'Result under "Total"
End Sub
So what I am doing right now is calculating the sum of all notional values, that are Type "A" or "B". 因此,我现在要做的是计算“ A”或“ B”类型的所有名义值的总和。 But for the final application this is not sufficient because now I am defining every single filter criteria in the code (fiterA, filterB).
但是对于最终应用程序来说,这还不够,因为现在我要在代码中定义每个过滤器标准(fiterA,filterB)。 The user should be able to add filter criteria in the table like:
用户应该能够在表中添加过滤条件,例如:
Filter Notional Type Total
A 5000 A _____
B 1000 A
C 2500 B
3600 C
7500 C
8000 B
4500 C
700 C
Right now I would have to prepare the code and add another criteria for the if condition, which is not desirable in the final application. 现在,我将不得不准备代码并为if条件添加另一个条件,这在最终应用程序中是不希望的。
I hope you have any ideas to solve my problem, Thanks! 希望您有解决我问题的想法,谢谢!
So instead of "hard coding" each filter cell in the IF statement, I loop through the filter column. 因此,我不遍历IF语句中的每个过滤器单元,而是遍历过滤器列。
For each g
(filter value in range "Filter"), it will check for every row if the criteria is found in column "Type" (Column C
). 对于每个
g
(“过滤器”范围内的过滤器值),它将检查是否在“类型”列( C
列)中找到了标准的每一行。 If match is found it will sum. 如果找到匹配项,它将相加。 When one filter criteria is done (
g
) it will go to next row in "Filter" column. 完成一个过滤条件(
g
)后,它将转到“过滤器”列中的下一行。
Sub filtersum()
Dim filterA As Variant, filterB As Variant
filterA = Cells(2, 1)
filterB = Cells(3, 1)
Dim k As Long, n As Long, h As Long
k = Cells.Find("Type").Offset(1, 0).Row
n = Cells.Find("Type").End(xlDown).Row
h = Cells.Find("Filter").End(xlDown).Row 'last row for column A, searching for "Filter"
Dim sum As Double
sum = 0 'Sum variable
Dim g As Long, i As Long, j As Long
Dim filterCriteria As Variant 'Define your criteria it should look up
For g = k To h 'Look up for Filter column, from the same start cell as "Type" column to the same end cell as "Type". Then loop through all criterias.
filterCriteria = Cells(g, 1).Value 'Get current criteria value
For i = k To n 'Loop through column Type
If Cells(i, 3) = filterCriteria Then 'If Type = filter criteria, then sum.
j = Cells(i, 2) 'I think you have an typo in your example here, since sum should be done on column B, not column C.
sum = j + sum 'Sum the values.
End If
Next i 'take next value in column C, "Type" column
Next g 'take next value in column A, "Filter" column
Cells(2, 4).Value = sum 'Result under "Total"
End Sub
you can use AutoFilter()
method with xlFilterValues
as its Operator
parameter value and WorksheetFunction.Subtotal()
function to sum up filtered cells, as follows 您可以使用带有
xlFilterValues
作为其Operator
参数值的AutoFilter()
方法和WorksheetFunction.Subtotal()
函数来汇总过滤后的单元格,如下所示
Option Explicit
Sub filtersum()
Dim filters As Variant
filters = Application.Transpose(Range("A2", Cells(Rows.Count, 1).End(xlUp)).Value)
With Range("B1", Cells(Rows.Count, 3).End(xlUp))
.AutoFilter Field:=2, Criteria1:=filters, Operator:=xlFilterValues
Range("D2").Value = Application.WorksheetFunction.Subtotal(9, .Columns(1))
End With
ActiveSheet.AutoFilterMode = False
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.