Excel VBA - 工作表 Class 的 ShowAllData 方法失败

Excel VBA - ShowAllData method of Worksheet Class failed

我已经自动将正确的记录输入到我用作数据库的 table 中,当 table 被过滤时,输入不起作用。

因此我编写了此代码以在每次记录输入之前取消过滤数据库。

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").ShowAllData
    DoEvents
    Sheets(ActiveS).Activate

Application.ScreenUpdating = CurrScreenUpdate
End Sub

但现在,它停留在 Sheets("DB").ShowAllData 上说:

ShowAllData method of Worksheet Class failed

因为 table 已经未经过滤...

而且我不知道使用像On Error Resume Next这样的错误处理程序或者如何检测是否有过滤器更好或 none.

欢迎大家指点!

如果您使用 Worksheet.AutoFilter.ShowAllData 而不是 Worksheet.ShowAllData,则在未过滤任何内容时不会抛出错误。

这假设 Worksheet.AutoFilterMode = True 因为否则你会得到关于 AutoFilter 不是对象的错误。

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

这是我使用的 工作解决方案 :

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

还有一个 更高效的版本 我写的是可以通过命名范围轻松重用:

使用方法:

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

适当的函数 优化(如果你有大表):

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
With ActiveSheet
     If .AutoFilterMode = False Then .Cells(1, 1).AutoFilter
     For Each f In .AutoFilter.Filters
          If f.On Then .ShowAllData: Exit For
Next