Excel macro filter based on multiple cell values

泪湿孤枕 提交于 2019-12-24 01:19:33

问题


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:


  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


来源:https://stackoverflow.com/questions/51204763/excel-macro-filter-based-on-multiple-cell-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!