vba 从表中清除过滤器

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

Clear filter from Table

excelvbaexcel-vba

提问by Dunn

I'm trying to run a macro that replaces data in a table in Excel, when the data might initially be filtered.

我正在尝试运行一个宏来替换 Excel 中表格中的数据,此时数据最初可能会被过滤。

The code should remove the filter first, then run the rest of the code.

代码应该先删除过滤器,然后运行其余的代码。

I tried the "Autofilter" command, but then the normal filter option on the table's range weren't visible, and I had to manually create the filter option again (not a big deal to me, but other people use this file).

我尝试了“自动过滤器”命令,但随后表格范围内的普通过滤器选项不可见,我不得不再次手动创建过滤器选项(对我来说没什么大不了,但其他人使用此文件)。

Is there a way to clear the filter WITHOUT removing filters from the table?

有没有办法清除过滤器而不从表中删除过滤器?

回答by Rory

For a Table, you can simply use the ShowAllDatamethod of its Autofilterobject:

对于一个表,你可以简单地使用ShowAllData它的Autofilter对象的方法:

activesheet.listobjects(1).autofilter.showalldata

Note this won't error even if there is no filter currently applied.

请注意,即使当前没有应用过滤器,这也不会出错。

回答by Prashant Mallick

Hi Guys use this "Worksheets("Sheet1").ListObjects(1).Range.AutoFilter = False" and please note if already filter applied it will remove filter on the other hand if filter not applied before it will apply the filter.

嗨,伙计们使用这个“Worksheets("Sheet1").ListObjects(1).Range.AutoFilter = False”,请注意,如果已经应用了过滤器,如果在应用过滤器之前没有应用过滤器,它将删除过滤器。

回答by AndyH

I am finding that the "...ClearAllData" method fails.

我发现“...ClearAllData”方法失败。

Sneaky - not hugely elegant solution - that works by field (so cumbersome if you need to do the whole table), but easy if you just have one field (e.g. field 2 in my example) is to use the wildcard:

偷偷摸摸 - 不是非常优雅的解决方案 - 按字段工作(如果您需要完成整个表格,那么麻烦),但如果您只有一个字段(例如我的示例中的字段 2),则很容易使用通配符:

ActiveSheet.ListObjects(1).Range.AutoFilter Field:=2, Criteria1:="=*"

回答by Alex Hedley

ActiveSheet.ShowAllData

Or

或者

Cells.AutoFilter