Excel 2013 VBA Clear All Filters macro

后端 未结 26 856
花落未央
花落未央 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:00

    Here's the one-liner I use. It checks for an auto-filter and if found, removes it.

    Unlike some answers, this code won't create an auto-filter if used on a worksheet that is not auto-filtered in the first place.

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

    If the sheet already has a filter on it then:

    Sub Macro1()
        Cells.AutoFilter
    End Sub
    

    will remove it.

    0 讨论(0)
  • 2020-11-27 17:01

    I found this workaround to work pretty effectively. It basically removes autofilter from the table and then re-applies it, thus removing any previous filters. From my experience this is not prone to the error handling required with the other methods mentioned here.

    Set myTable = YOUR_SHEET.ListObjects("YourTableName")
    
    myTable.ShowAutoFilter = False
    myTable.ShowAutoFilter = True
    
    0 讨论(0)
  • 2020-11-27 17:04

    Try something like this:

    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 returns true if the worksheet is in filter mode. (See this for more information.)
    See this for more information on .AutoFilter.
    And finally, this will provide more information about the .ShowAllData method.

    0 讨论(0)
  • 2020-11-27 17:04

    Im using .filtermode if filter is on it returns true

    Dim returnValue As Boolean
        returnValue = worksheet1.FilterMode
    
        if returnValue Then
        worksheet1.ShowAllData
        End If
    
    0 讨论(0)
  • 2020-11-27 17:06

    Simply activate the filter headers and run showalldata, works 100%. Something like:

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

    If you have the field headers in A1:Z1 and R1:Y1 respectively.

    0 讨论(0)
提交回复
热议问题