Excel 2013 VBA Clear All Filters macro

后端 未结 26 855
花落未央
花落未央 2020-11-27 16:26

It seems older macros are not working. I have proper securtiy set to run VBA macros but when I have tried a few methods for clearing ALL filters on a worksheet, I get a comp

相关标签:
26条回答
  • 2020-11-27 17:08

    this works nice.!

    If ActiveSheet.AutoFilterMode Then Cells.AutoFilter
    
    0 讨论(0)
  • 2020-11-27 17:09

    For tables try this to check if it's on and turn off:

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

    To Turn back on:

    wrkSheetCodeName.ListObjects("TableName").Range.AutoFilter
    
    0 讨论(0)
  • 2020-11-27 17:09

    That is brilliant, the only answer I found that met my particular need, thanks SO much for putting it up!

    I made just a minor addition to it so that the screen didn't flash and it removes and subsequently reapplies the password on each sheet as it cycles through [I have the same password for all sheets in the workbook]. In the spirit of your submission, I add this to assist anyone else....

    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
    

    I know this is a relatively old post and don't really like being a necromancer... But since I had the same issue and tried a few of the options in this thread without success I combined some of the answers to get a working macro..

    Hopefully this helps someone out there :)

    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
    
    0 讨论(0)
  • 2020-11-27 17:10

    Here is some code for fixing filters. For example, if you turn on filters in your sheet, then you add a column, then you want the new column to also be covered by a 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
    
    0 讨论(0)
  • 2020-11-27 17:11

    This thread is ancient, but I wasn't happy with any of the given answers, and ended up writing my own. I'm sharing it now:

    We start with:

      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
    

    We can feed a specific worksheet to this macro which will unfilter just that one worksheet. Useful if you need to make sure just one worksheet is clear. However, I usually want to do the entire workbook

    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
    

    You can use this, by, for example, opening a workbook you need to deal with and resetting their filters before doing anything with it:

    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
    

    The one I use the most: Resetting all filters in the workbook that the module is stored in:

    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
    
    0 讨论(0)
  • 2020-11-27 17:13

    This will work too:

    If ActiveSheet.FilterMode Then cells.AutoFilter
    
    0 讨论(0)
提交回复
热议问题