vba Excel VBA中的多重过滤?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12040906/
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-08 13:49:59  来源:igfitidea点击:

Multiple Filtering in Excel VBA?

excelexcel-vbavba

提问by K20GH

I'm trying to filter my data using 2 columns and 2 different filter requirements.

我正在尝试使用 2 列和 2 个不同的过滤器要求过滤我的数据。

    ActiveSheet.AutoFilterMode = False
    ActiveSheet.Range("$K:$ZZ0").AutoFilter Field:=5, Criteria1:="yes"
    ActiveSheet.Range("E:E").AutoFilter Field:=1, Criteria1:="Low"

If I filter by yes then it work and if I filter by Low it also works. However if I try and filter range K2-ZZ200 by yes and then Column E by Low it seems to hide everything.

如果我按 yes 过滤,那么它可以工作,如果我按 Low 过滤,它也可以工作。但是,如果我尝试按是过滤范围 K2-ZZ200,然后按低过滤 E 列,它似乎隐藏了所有内容。

Any ideas?

有任何想法吗?

采纳答案by Ryan

You have to use the same Range("$K$2:$ZZ$200")for both AutoFilterstatements:

您必须Range("$K$2:$ZZ$200")对两个AutoFilter语句使用相同的语句:

' Column A = Field 1
ActiveSheet.Range("$K:$ZZ0").AutoFilter Field:=1, Criteria1:="yes"
' Column E = Field 5
ActiveSheet.Range("$K:$ZZ0").AutoFilter Field:=5, Criteria1:="Low"

Also, you may need to set ActiveSheet.AutoFilterMode = True, but I'm not entirely sure.

此外,您可能需要设置ActiveSheet.AutoFilterMode = True,但我不完全确定。

回答by K20GH

Almost. Managed to do this using an array. The Range does not need to be the same by the way.

几乎。设法使用阵列来做到这一点。顺便说一下,范围不需要相同。

     ActiveSheet.AutoFilterMode = False
    ActiveSheet.Range("$E:$ZZ0").AutoFilter Field:=nice, Criteria1:="yes"
    ActiveSheet.Range("$E:$ZZ0").AutoFilter Field:=1, Criteria1:=Array( _
    "Filter 1", ""Filter 2", "On "Filter 3", ""Filter 4", ""Filter 5"), Operator:=xlFilterValues