我已经将一个正确的记录输入自动化到我用作数据库的表中,当表被过滤时,输入不起作用。
所以我在每次录制输入之前都要对此进行编码以解析DataBase。
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方法失败
因为该表已经未经过滤...
我不知道使用错误处理程序(如On Error Resume Next
)或检测是否有过滤器或没有更好。
欢迎任何指示!
答案 0 :(得分:3)
如果您使用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
答案 1 :(得分:1)
以下是工作解决方案:
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
答案 2 :(得分:-1)
With ActiveSheet
If .AutoFilterMode = False Then .Cells(1, 1).AutoFilter
For Each f In .AutoFilter.Filters
If f.On Then .ShowAllData: Exit For
Next