问题
I have this simple macro that filters rows based on value in A13 cell. It works fine.
With ActiveSheet
.Range("B2:F5000").AutoFilter Field:=2, Criteria1:=.Range("A13")
End With
But I need more values to be applied for this filter, specifically based on two or more cells. So I run this macro:
With ActiveSheet
.Range("B2:F5000").AutoFilter Field:=2, Criteria1:=.Range("A13:A14:A15")
End With
But it filters only values based on A15 cell. Why is that? I have read all the topics here, but no solution to this specific problem. Thank you all for your help. Libor.
回答1:
- Remove the old AutoFilter first
- Filter on
Field:=1
. If your range starts in column B and you want to filter in column B then this is the first field not the second. - If you want to filter on values/numbers not text, filter by using a formula instead of the value. Eg
=200
to filter for number200
.
Here is an Example that should work.
With ActiveSheet 'better reference a sheet by its name like: Worksheets("Sheet1")
If .AutoFilterMode = True Then .AutoFilterMode = False 'remove old autofilter
.Range("B:F").AutoFilter Field:=1, Operator:=xlFilterValues, _
Criteria1:=Array("=" & .Range("A13").Value, "=" & .Range("A14").Value, "=" & .Range("A15").Value)
End With
来源:https://stackoverflow.com/questions/51204763/excel-macro-filter-based-on-multiple-cell-values