[英]Excel macro for multiple string autofilter
I am trying to add code to a macro to autofilter based on if a specific column contains one of several strings. 我正在尝试将代码添加到宏中,以便根据特定列是否包含多个字符串之一来自动过滤。 Here's what I did for 2 strings (R is the range):
这是我对2个字符串所做的操作(R是范围):
R.AutoFilter Field:=ProductTypeCol, _
Criteria1:=Array("*maintenance*", "*services*"), _
Operator:=xlFilterValues
This works exactly as I expected, and shows some rows for each string. 这完全符合我的预期,并为每个字符串显示了一些行。 Then I added a third string and re-executed the macro:
然后,我添加了第三个字符串并重新执行了宏:
R.AutoFilter Field:=ProductTypeCol, _
Criteria1:=Array("*maintenance*", "*services*", "*training*"), _
Operator:=xlFilterValues
Instead of showing some additional rows, this mysteriously results in no rows at all. 而不是显示其他行,这神秘地导致根本没有行。
So in search of truth, I removed the asterisks from all three strings and got exactly what I expected: rows where the value was exactly one of those strings. 因此,为了寻找真相,我从所有三个字符串中删除了星号,并得到了我所期望的:值恰好是这些字符串之一的行。 Unfortunately, I really need those asterisks.
不幸的是,我真的需要那些星号。
Am I missing something here? 我在这里想念什么吗?
You cannot filter more than two criteria with wildcards. 您不能使用通配符过滤两个以上的条件。 If you want to do that, you'll have to add a temporary helper column, or filter with a loop, or another workaround.
如果要这样做,则必须添加一个临时帮助器列,或使用循环进行过滤,或采用其他解决方法。
As far as I am aware of, you cannot filter on the same column by more than two criteria with wildcard. 据我所知,您不能使用通配符在同一列上通过两个以上的条件进行过滤。 I think what you would need to do is to move the filtering requirement into an additional column populated by a formula along the lines of:
我认为您需要做的是将过滤要求移到由公式填充的附加列中,其内容如下:
Assuming ProductTypeCol
is column A. 假设
ProductTypeCol
在A列中。
=SUM(COUNTIF($A2,{"=*maintenance*","=*services*","=*training*"}))>0
Then filter on this column for the Trues (you can extend this to more search terms if required) 然后在此列上过滤True(您可以根据需要将其扩展到更多搜索词)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.