Excel VBA - ShowAllData method of Worksheet Class failed

非 Y 不嫁゛ 提交于 2019-12-04 19:59:33
Byron Wall

If you use Worksheet.AutoFilter.ShowAllData instead of Worksheet.ShowAllData it will not throw the error when nothing is filtered.

This assumes that Worksheet.AutoFilterMode = True because otherwise you will get an error about AutoFilter not being an object.

Public Sub UnFilter_DB()
Dim ActiveS As String, CurrScreenUpdate As Boolean

CurrScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
ActiveS = ActiveSheet.Name

    Sheets("DB").Activate
    Sheets("DB").Range("A1").Activate
    Sheets("DB").AutoFilter.ShowAllData
    DoEvents
    Sheets(ActiveS).Activate

Application.ScreenUpdating = CurrScreenUpdate
End Sub

Here is the working solution I went with :

Public Sub UnFilter_DB()
Dim ActiveS As String, CurrScreenUpdate As Boolean

CurrScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
ActiveS = ActiveSheet.Name

    Sheets("DB").Activate
    Sheets("DB").Range("A1").Activate
    On Error Resume Next
    If Sheets("DB").FilterMode = True Then Sheets("DB").ShowAllData
    On Error GoTo 0

    DoEvents
    Sheets(ActiveS).Activate

Application.ScreenUpdating = CurrScreenUpdate
End Sub

And a more efficient version I wrote to be reusable easily with Named Ranges :

How to use it :

Private Sub TEST_UnFilter_Table()
    Dim tB As Workbook, _
        Sh As Worksheet

    Set tB = ThisWorkbook
    Set Sh = tB.Sheets("DB")

    Call UnFilter_Table(Sh, "Db_Val")
End Sub

And the proper function with optimisation (if you have big tables) :

Public Function UnFilter_Table(ByRef SheetWithTable As Worksheet, ByVal RangeName As String) As Boolean

On Error GoTo ErrHdlr
    Dim aWB As Workbook, _
        ActiveSH As Worksheet, _
        ScreenUpdateState As Boolean, _
        StatusBarState As Boolean, _
        CalcState As XlCalculation, _
        EventsState As Boolean, _
        DisplayPageBreakState As Boolean

    Set aWB = ActiveWorkbook
    Set ActiveSH = aWB.ActiveSheet

    DisplayPageBreakState = ActiveSH.DisplayPageBreaks
    ActiveSH.DisplayPageBreaks = False

    With Application
        ScreenUpdateState = .ScreenUpdating
        StatusBarState = .DisplayStatusBar
        CalcState = .Calculation
        EventsState = .EnableEvents

        .ScreenUpdating = False
        .DisplayStatusBar = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With


    SheetWithTable.Activate
    SheetWithTable.Range(RangeName).Cells(1, 1).Activate
On Error GoTo 0

On Error Resume Next
    If SheetWithTable.FilterMode Then SheetWithTable.ShowAllData
On Error GoTo 0

On Error GoTo ErrHdlr
    DoEvents
    ActiveSH.Activate
    ActiveSH.DisplayPageBreaks = DisplayPageBreakState

    With Application
        .ScreenUpdating = ScreenUpdateState
        .DisplayStatusBar = StatusBarState
        .Calculation = CalcState
        .EnableEvents = EventsState
    End With

    UnFilter_Table = True
On Error GoTo 0

Exit Function
ErrHdlr:
UnFilter_Table = False
Debug.Print "Error in unfiltering sheet " & SheetWithTable.Name & " !" & vbCrLf & _
            "Error n° " & Err.Number & vbCrLf & _
            Err.Description
End Function
Alt
With ActiveSheet
     If .AutoFilterMode = False Then .Cells(1, 1).AutoFilter
     For Each f In .AutoFilter.Filters
          If f.On Then .ShowAllData: Exit For
Next
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!