Lajbror
Lajbror

Reputation: 45

Excel macro filter based on multiple cell values

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.

Upvotes: 3

Views: 10497

Answers (2)

Jumong
Jumong

Reputation: 1

Try with:

.Range("B2:F5000").AutoFilter Field:=2, Criteria1:=Array(Range("A13"), Range("A14"), Range("A15"))

Upvotes: 0

Pᴇʜ
Pᴇʜ

Reputation: 57683

  1. Remove the old AutoFilter first
  2. 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.
  3. If you want to filter on values/numbers not text, filter by using a formula instead of the value. Eg =200 to filter for number 200.

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

Upvotes: 2

Related Questions