简体   繁体   English

Excel VBA:聪明地使用自动筛选

[英]Excel VBA: using autofilter smartly

So, here's my question... 所以,这是我的问题...

I have a column with strings looking like this: 我有一列的字符串看起来像这样:

"2,3" "1,2" "2" "3,22,1" "32,5" “ 2,3”“ 1,2”“ 2”“ 3,22,1”“ 32,5”

Now, what I would like to do is to find a way (without writing my own function for it) to filter everything that doesn't contain "2" (the rows with "32" or "22" need to be filtered out). 现在,我想做的是找到一种方法(无需为其编写自己的函数)来过滤不包含“ 2”的所有内容(“ 32”或“ 22”的行需要过滤掉) 。

So, for example, by keeping only "2", from the list above, I would like to get: "2,3" "1,2" "2" 因此,例如,通过仅保留上面列表中的“ 2”,我想得到:“ 2,3”,“ 1,2”,“ 2”

I am now filtering using: 1. contains "2," OR contains ",2" OR equals "2" 我现在使用以下内容进行过滤:1.包含“ 2”,或包含“,2”或等于“ 2”

Unfortunately, "3,22,1" will be kept by the first two filters and "32,5" is kept by the second one. 不幸的是,“ 3,22,1”将由前两个过滤器保留,而“ 32,5”将由第二个过滤器保留。

Any ideas on how to do this with the filter function? 关于如何使用过滤功能执行此操作的任何想法? Like I said, I can write a function for it, but I'd keep my code minimal and learn how to use VBA better, instead. 就像我说的那样,我可以为其编写函数,但是我将使代码最少并学习如何更好地使用VBA。

Thank you in advance! 先感谢您! Riccardo 里卡多

I'm still kind of unsure what you want to remove and what to keep, but using your array of criteria, how about this: 我仍然不确定要删除的内容和要保留的内容,但是使用您的一系列条件,如何做到这一点:

Sheets("Sheet1").Range("A:A").AutoFilter Field:=1, Criteria1:=Array("2,", ",2","2",",2,"),_ 
Operator:=xlFilterValues

Worth noting it's a more foolproof way when writing macros to refer to the sheet directly otherwise they can occasionally get lost in the script :) 值得注意的是,编写宏直接直接引用工作表时,这是一种更简单的方法,否则它们有时会在脚本中迷路:)

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

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