vba 在自动筛选中组合多个排除 (<>) 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7662659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:11:52  来源:igfitidea点击:

Combine multiple exclusion (<>) criteria in AutoFilter

excelvbaexcel-vbaautofilter

提问by denisq

I have worked around my issue by using this dirty hack:

我通过使用这个肮脏的黑客解决了我的问题:

    ' Filter managerial functions
    ActiveSheet.Range("$A:$BW11").AutoFilter Field:=36, Criteria1:="<>Head*", _
    Criteria2:="<>IT*", Operator:=XlAutoFilterOperator.xlAnd
    ActiveSheet.Range("$A:$BW11").AutoFilter Field:=36, Criteria1:="<>Local Head*", _
    Criteria2:="<>Resp*", Operator:=XlAutoFilterOperator.xlAnd
    ActiveSheet.Range("$A:$BW11").AutoFilter Field:=36, Criteria1:="<>Team Lead*", _
    Criteria2:="<>XB*", Operator:=XlAutoFilterOperator.xlAnd

Is there any way to combine these 3 statements into one line? Excel seems to have a problem as soon as I have a third criteria (Criteria3) in one line. Furthermore, <>Array() seems not to be supported.

有没有办法将这 3 条语句合并为一行?一旦我在一行中有第三个条件 (Criteria3),Excel 似乎就会出现问题。此外, <>Array() 似乎不受支持。

回答by Jon49

An advanced filter might be more suitable for this purpose.

高级过滤器可能更适合此目的。

You could also do something like this:

你也可以做这样的事情:

Dim bUnion As Boolean
Dim i As Long
Dim vData As Variant
Dim rDataHide As Range

vData = Application.Transpose(ActiveSheet.Range("$AJ:$AJ11"))
bUnion = False

For i = 1 To 2211
  If LenB(vData(i)) Then
    If vData(i) Like Whatever Or vData(i) Like Whatever2 Then
      If bUnion Then
        Set rDataHide = Union(rDataHide, ActiveSheet.Range("$AJ$" & i))
      Else
        Set rDataHide = ActiveSheet.Range("$AJ$" & i)
        bUnion = True
      End If
    End If
  End If
Next i
rDataHide.Rows.Hidden = True

You could even use RegEx, I haven't really used RegEx much before though so you would have to google it.

您甚至可以使用 RegEx,但我之前并没有真正使用过 RegEx,所以您必须在 google 上搜索它。