简体   繁体   English

Excel:使用多个条件筛选:AutoFilter或AdvancedFilter

[英]Excel : filter with Multiple Criteria : AutoFilter or AdvancedFilter

I am trying to create a filter that will do the below 我正在尝试创建一个将执行以下操作的过滤器

Begins with "4" or Contains "2TWH" or Contains "2TER" 以“4”开头或包含“2TWH”或包含“2TER”

I believe it will turn out to look like this 我相信它会变成这样

ActiveSheet.Range("$A$5:$H$10").AutoFilter Field:=2, Criteria1:="=4*", _
    Operator:=xlOr, Criteria2:="=*2TWH*" 

but I need to somehow also have a criteria 3 for ="= 2TER " 但我需要以某种方式对=“= 2TER ”有一个标准3

I did some googling and saw that you can store the values into an array and then call the array values but I was unable to get them into the array in a usable fashion. 我做了一些谷歌搜索,看到你可以将值存储到一个数组,然后调用数组值,但我无法以可用的方式将它们放入数组。

Is anyone able to offer some assistance with this please? 有人能为此提供一些帮助吗?

This should do the trick : 这应该做的伎俩:

ActiveSheet.Range("$A$5:$H$10").AutoFilter Field:=2, Criteria1:="=4*", _
    Operator:=xlOr, Criteria2:=Array("*2TWH*","*2TER*")

This doesn't throw any error, but ... 这不会引起任何错误,但......
it'll only take 2 criteria because there are wildcards ( * ). 它只需要2个标准,因为有通配符( * )。
Here it's the last criteria from the array (here *2TER* ) 这是数组中的最后一个条件(这里是*2TER*


Ideally, this would have been nice, as it works for constants , 理想情况下,这会很好,因为它适用于常量
but because you use WildCards ( * ) it only supports 2 criteria... :/ 但因为你使用WildCards( * )它只支持2个标准......:/

ActiveSheet.Range("$A$1:$H$10").AutoFilter Field:=2, Criteria1:=Array("*2TWH*", "*2TER*", "4*"), _
    Operator:=xlFilterValues


So you'll have to go with AdvancedFilter : 所以你必须使用AdvancedFilter

With ActiveSheet
    'Const xlFilterInPlace = 1
    .Range("$A$5:$H$10").AdvancedFilter _
                        Action:=xlFilterInPlace, _
                        CriteriaRange:=WsFilterSettings.Range("A1:A4")
End With 'ActiveSheet

AdvancedFilter take a Range as criteria inputs , CriteriaRange , so you'll have to put on a sheet : AdvancedFilterRange作为条件输入CriteriaRange ,因此您必须放置一个工作表

  • the header of the column you want to apply to the filter on 要应用于过滤器的列标题
  • your criteria below the respective header (if you have multiples columns) 您的标准低于相应的标题(如果您有多个列)

Each COLUMN of that Range, are linked by an AND 该范围的每个COLUMNAND 链接
Each ROW of that Range, are linked by an OR 该范围的每个ROW都通过OR 链接
So build your table carefully! 所以仔细建立你的桌子!

In the above example code, I have used : 在上面的示例代码中,我使用了:
(let's say that your column's header was Column To Filter On ) : (假设您的列的标题是“ 要过滤的列” ):

A1 | Column To Filter On
A2 | 4*
A3 | *2TWH*
A4 | *2TER*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM