Excel 2013 VBA清除所有筛选器macros

看来,旧的macros不起作用。 我有适当的安全设置运行VBAmacros,但是当我已经尝试了几个方法清除工作表上的所有筛选器,我得到一个编译错误。

这是我所尝试的:

Sub AutoFilter_Remove() 'This macro removes any filtering in order to display all of the data but it does not remove the filter arrows ActiveSheet.ShowAllData End Sub 

我有工作表上的button来清除所有filter,方便用户使用,因为工作表中有许多列上有filter的列。

如果表单上已经有filter,则:

 Sub Macro1() Cells.AutoFilter End Sub 

将删除它。

尝试这个:

 If ActiveSheet.AutoFilterMode Then ActiveSheet.ShowAllData 

对于表格,请尝试检查它是否打开并closures:

 If wrkSheetCodeName.ListObjects("TableName").ShowAutoFilter Then wrkSheetCodeName.ListObjects("TableName").Range.AutoFilter End if 

要恢复:

 wrkSheetCodeName.ListObjects("TableName").Range.AutoFilter 

如果当前没有应用filter,则ShowAllData将引发错误。 这将工作:

 Sub ResetFilters() On Error Resume Next ActiveSheet.ShowAllData End Sub 

这个作品很好。

 If ActiveSheet.AutoFilterMode Then Cells.AutoFilter 

我发现这个解决方法非常有效地工作。 它基本上从表中删除自动filter,然后重新应用它,从而删除任何以前的filter。 从我的经验来看,这里提到的其他方法不容易出错。

 Set myTable = YOUR_SHEET.ListObjects("YourTableName") myTable.ShowAutoFilter = False myTable.ShowAutoFilter = True 

这也将工作:

 If ActiveSheet.FilterMode Then cells.AutoFilter End If 

这将首先检查是否设置了AutoFilterMode(可以过滤),然后检查FilterMode是否打开(您正在过滤某些内容),然后closures过滤。

关于错误,即保护 – 其他答案

添加了上下文(我的脚本循环遍历表单,然后保存为CSV,因此需要删除filter – 但保持AutoFilterMode,如果设置:

 For Each WS In ActiveWorkbook.Worksheets Select Case WS.Name Case "01", "02", "03", "04", "05" With WS If WS.AutoFilterMode Then If WS.FilterMode Then WS.ShowAllData End If ' Processing data End With Case Else ' Nothing to see here End Select Next 

我通常使用这个代码

 Sub AutoFilter_Remove() Sheet1.AutoFilterMode = False 'Change Sheet1 to the relevant sheet 'Alternatively: Worksheets("[Your Sheet Name]").AutoFilterMode = False End Sub 

尝试这样的事情:

 Sub ClearDataFilters() 'Clears filters on the activesheet. Will not clear filters if the sheet is protected. On Error GoTo Protection If ActiveWorkbook.ActiveSheet.FilterMode Or _ ActiveWorkbook.ActiveSheet.AutoFilterMode Then _ ActiveWorkbook.ActiveSheet.ShowAllData Exit Sub Protection: If Err.Number = 1004 And Err.Description = _ "ShowAllData method of Worksheet class failed" Then MsgBox "Unable to Clear Filters. This could be due to protection on the sheet.", _ vbInformation End If End Sub 

.FilterMode在工作表处于过滤模式时返回true。 ( 有关更多信息, 请参阅此部分 。)
请参阅 .AutoFilter更多信息。
最后, 这将提供更多有关.ShowAllData方法的信息。

这是我使用的单线。 它检查一个自动filter,如果find,将其删除。

与一些答案不同,这个代码不会创build一个自动filter,如果在一个没有自动过滤的工作表上使用的话。

 If Cells.AutoFilter Then Cells.AutoFilter 

那真是太棒了,唯一的答案就是满足了我的特殊需求,非常感谢你的支持!

我做了一个小小的补充,以便屏幕不会闪烁,并随着循环[我有相同的工作簿中的所有工作表的密码],它将删除并随后重新应用每张表上的密码。 本着您提交的精神,我将其添加到协助其他人….

Sub ClearFilters()

Application.ScreenUpdating = False

 On Error Resume Next For Each wrksheet In ActiveWorkbook.Worksheets ' Change the password to whatever is required wrksheet.Unprotect Password:="***" wrksheet.ShowAllData 'This works for filtered data not in a table For Each lstobj In wrksheet.ListObjects If lstobj.ShowAutoFilter Then lstobj.Range.AutoFilter 'Clear filters from a table lstobj.Range.AutoFilter 'Add the filters back to the table End If ' Change the password to whatever is required wrksheet.Protect Password:="***", _ DrawingObjects:=True, _ Contents:=True, _ Scenarios:=True, _ AllowFiltering:=True Next 'Check next worksheet in the workbook Next Application.ScreenUpdating = True End Sub 

我知道这是一个相对较旧的职位,并不真正喜欢作为一个巫师……但因为我有同样的问题,并试图在这个线程中的一些选项没有成功,我结合了一些答案,以获得工作的macros..

希望这可以帮助别人:)

 Sub ResetFilters() On Error Resume Next For Each wrksheet In ActiveWorkbook.Worksheets wrksheet.ShowAllData 'This works for filtered data not in a table For Each lstobj In wrksheet.ListObjects If lstobj.ShowAutoFilter Then lstobj.Range.AutoFilter 'Clear filters from a table lstobj.Range.AutoFilter 'Add the filters back to the table End If Next 'Check next worksheet in the workbook Next End Sub 

只需激活filter标题并运行showalldata,工作100%。 就像是:

 Range("A1:Z1").Activate ActiveSheet.ShowAllData Range("R1:Y1").Activate ActiveSheet.ShowAllData 

如果在A1:Z1和R1:Y1中分别有字段标题。

我使用.filtermode如果filter上它返回true

 Dim returnValue As Boolean returnValue = worksheet1.FilterMode if returnValue Then worksheet1.ShowAllData End If 

尝试这个:

 Sub ResetFilters() Dim ws As Worksheet Dim wb As Workbook Dim listObj As ListObject For Each ws In ActiveWorkbook.Worksheets For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj Next ws End Sub 

此代码清除所有filter并删除sorting。

源: 删除工作簿,VBA中的每个表的筛选器

这里是一些代码来修复filter。 例如,如果您打开工作表中的filter,然后添加一列,那么您希望新的列也被filter覆盖。

 Private Sub AddOrFixFilters() ActiveSheet.UsedRange.Select ' turn off filters if on, which forces a reset in case some columns weren't covered by the filter If ActiveSheet.AutoFilterMode Then Selection.AutoFilter End If ' turn filters back on, auto-calculating the new columns to filter Selection.AutoFilter End Sub 

这个线程是古老的,但我对任何给定的答案都不满意,最后写了我自己的答案。 我现在分享它:

我们从以下开始:

  Sub ResetWSFilters(ws as worksheet) If ws.FilterMode Then ws.ShowAllData Else End If 'This gets rid of "normal" filters - but tables will remain filtered For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj 'And this gets rid of table filters End Sub 

我们可以将一个特定的工作表提供给这个macros,这个macros将不会只是一个工作表。 如果您需要确保只有一个工作表是清楚的,这很有用。 但是,我通常要做整个工作簿

 Sub ResetAllWBFilters(wb as workbook) Dim ws As Worksheet Dim wb As Workbook Dim listObj As ListObject For Each ws In wb.Worksheets If ws.FilterMode Then ws.ShowAllData Else End If 'This removes "normal" filters in the workbook - however, it doesn't remove table filters For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj Next 'And this removes table filters. You need both aspects to make it work. End Sub 

你可以使用它,例如打开一个你需要处理的工作簿,并在做任何事情之前重置他们的filter:

 Sub ExampleOpen() Set TestingWorkBook = Workbooks.Open("C:\Intel\......") 'The .open is assuming you need to open the workbook in question - different procedure if it's already open Call ResetAllWBFilters(TestingWorkBook) End Sub 

我使用最多的一个:重置模块存储在工作簿中的所有filter:

 Sub ResetFilters() Dim ws As Worksheet Dim wb As Workbook Dim listObj As ListObject Set wb = ThisWorkbook 'Set wb = ActiveWorkbook 'This is if you place the macro in your personal wb to be able to reset the filters on any wb you're currently working on. Remove the set wb = thisworkbook if that's what you need For Each ws In wb.Worksheets If ws.FilterMode Then ws.ShowAllData Else End If 'This removes "normal" filters in the workbook - however, it doesn't remove table filters For Each listObj In ws.ListObjects If listObj.ShowHeaders Then listObj.AutoFilter.ShowAllData listObj.Sort.SortFields.Clear End If Next listObj Next 'And this removes table filters. You need both aspects to make it work. End Sub 

我在微软的网页上find了这个答案

它使用AutoFilterMode作为布尔值。

  If Worksheets("Sheet1").AutoFilterMode Then Selection.AutoFilter