首页 文章

Excel 2013 VBA清除所有过滤器宏

提问于
浏览
32

似乎较旧的宏不起作用 . 我有适当的securtiy设置来运行VBA宏但是当我尝试了一些方法来清除工作表上的所有过滤器时,我得到一个编译错误 .

这是我尝试过的:

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

我在工作表上有按钮以清除所有过滤器,以方便用户使用,因为工作表有很多列,上面有过滤器 .

23 回答

  • 0

    如果工作表上已有过滤器,则:

    Sub Macro1()
        Cells.AutoFilter
    End Sub
    

    将删除它 .

  • 0

    试试这个:

    If ActiveSheet.AutoFilterMode Then ActiveSheet.ShowAllData
    
  • 0

    如果当前未应用过滤器,ShowAllData将引发错误 . 这将有效:

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

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

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

    要重新开启:

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

    这很好用 .

    If ActiveSheet.AutoFilterMode Then Cells.AutoFilter
    
  • 2

    我发现这种解决方法非常有效 . 它基本上从表中删除自动过滤器,然后重新应用它,从而删除任何以前的过滤器 . 根据我的经验,这不容易出现这里提到的其他方法所需的错误处理 .

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

    这很棒,我找到的唯一答案符合我的特殊需求,非常感谢你提出这个问题!

    我做了一个小小的补充,以便屏幕没有闪烁,它删除并随后在每张纸上重新应用密码,因为它循环[我对工作簿中的所有工作表都有相同的密码] . 根据你提交的精神,我加上这个来帮助其他人......

    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:="Albuterol1"
            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:="Albuterol1", _
                  DrawingObjects:=True, _
                  Contents:=True, _
                  Scenarios:=True, _
                  AllowFiltering:=True
            Next 'Check next worksheet in the workbook
        Next
    
        Application.ScreenUpdating = True
    End Sub
    

    我知道这是一个相对古老的帖子,并不喜欢成为一个死灵法师...但是因为我有同样的问题,并尝试了这个线程中的一些选项而没有成功,我结合了一些答案来获得一个有效的宏..

    希望这有助于那里的人:)

    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
    
  • 2

    我通常使用这个代码

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

    这也可以:

    If ActiveSheet.FilterMode Then
    cells.AutoFilter
    End If
    
  • 5

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

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

    添加了上下文(我的脚本循环显示工作表,然后保存为CSV,因此需要删除过滤器 - 但保持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
    
  • 8

    Excel中有两种类型的过滤器:

    • 自动过滤器

    • 高级过滤器

    自动过滤功能允许您使用这些微小的下拉按钮从Excel界面进行过滤 . 高级过滤功能可让您使用条件范围进行过滤 .

    ShowAll方法删除过滤器,如,显示所有行,但不删除那些下拉按钮 . 您必须将工作表的AutoFilterMode属性设置为FALSE才能删除这些按钮 .

    这是我经常用来删除过滤器的Sub:

    Sub RemoveFilters(ByRef WhichSheet As Worksheet)
    
    If WhichSheet.FilterMode Then WhichSheet.ShowAllData
    If WhichSheet.AutoFilterMode Then WhichSheet.AutoFilterMode = False
    
    End Sub
    

    这将显示所有数据,并删除下拉按钮 . 它在从多个工作表或工作簿中堆叠(复制和粘贴)数据时非常方便 . 希望这可以帮助 .

  • 50

    尝试这样的事情:

    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 . (See this了解更多信息 . )
    See this了解 .AutoFilter 的更多信息 .
    最后,this将提供有关 .ShowAllData 方法的更多信息 .

  • 3

    这是我使用的单线 . 它会检查自动过滤器,如果找到,则将其删除 .

    与某些答案不同,如果在首先未自动过滤的工作表上使用此代码,则不会创建自动过滤器 .

    If Cells.AutoFilter Then Cells.AutoFilter
    
  • 0

    只需激活过滤器 Headers 并运行showalldata,即可100%运行 . 就像是:

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

    如果A1:Z1和R1:Y1中的字段 Headers 分别为 .

  • 0

    我使用 .filtermode 如果过滤器打开它返回true

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

    试试这个:

    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
    

    此代码清除所有过滤器并删除排序 .

    资料来源:Removing Filters for Each Table in a Workbook, VBA

  • 15

    这是一些修复过滤器的代码 . 例如,如果打开工作表中的过滤器,则添加一列,然后您希望新列也被过滤器覆盖 .

    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
    
  • 4

    这个帖子很古老,但我对任何给定的答案都不满意,最后写了自己的答案 . 我现在正在分享它:

    我们从:

    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
    

    我们可以将一个特定的工作表提供给这个宏,这个工作表将不会过滤掉那个工作表 . 如果你需要确保只有一个工作表是清楚的,这很有用 . 但是,我通常想要完成整个工作簿

    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
    

    您可以使用此方法,例如,在执行任何操作之前打开您需要处理的工作簿并重置其过滤器:

    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
    

    我最常用的一个:重置工作簿中存储模块的所有过滤器:

    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
    
  • 3

    所有你需要的是:

    ActiveSheet.AutoFilter.ShowAllData
    

    为什么?与工作表一样,AutoFilter也有一个ShowAllData方法,但即使在没有活动过滤器的情况下启用自动过滤器也不会引发错误 .

  • 0

    仅当您有过滤器时才会清除,并且在没有任何过滤器时不会导致任何错误 . 如果ActiveSheet.AutoFilterMode那么ActiveSheet.Columns( “A”) . 自动筛选

  • 1

    这对我来说效果最好 .

    在保存和关闭文件之前,我通常使用以下内容 .

    Sub remove_filters
    
    ActiveSheet.AutofilterMode = False
    
    End Sub
    
  • -2

    我在Microsoft webpage找到了这个答案

    它使用AutoFilterMode作为布尔值 .

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

    在使用 ActiveSheet.ShowAllData 之前,您必须先选择表格的范围

相关问题