范围类的自动筛选方法失败 – 表

我试图做到这一点,它运行良好,我得到的表格过滤的基础上,但它给了我错误:范围类的自动筛选方法失败。

我不确定是否使用正确的事件来触发此代码:

这里是最后一行给出错误的代码的一部分。

Private Sub ComboBox1_Change() Application.EnableEvents=False Dim wt As Worksheet Dim wib As Worksheet Dim i As Long, j As Long, frow As Long, ck As Boolean, scol As Long, ecol As Long Dim manName As String manName = Me.ComboBox1.Value Set wt = Sheet3 Set wib = Sheet9 'IB Skills Sheet wt.Activate wt.Range("A1") = 2 If Trim(manName) = "" Then MsgBox "Manager Selected is Invalid" Exit Sub End If wib.Activate wib.Range("C2").Select wib.AutoFilterMode = False wib.ListObjects("Table1").Range.AutoFilter Field:=3, Criteria1:= _ manName 

我也试过这个,但是我得到了另一个错误:

 wib.AutoFilterMode = False frow = wib.Range("A" & Rows.Count).End(xlUp).Row wib.Range("A1:BH:" & frow).AutoFilter Field:=3, Criteria1:=manName 

有趣的是,如果我把Debug.Print "Blah Blah..."放在这之前,我会在“立即”窗口中打印两次。

 wib.ListObjects("Table1").Range.AutoFilter Field:=3, Criteria1:= _ manName 

我不知道为什么是这样? 也许我在这里使用错误的事件处理程序? 需要你的帮助。 谢谢。

例:

如果我使用这个:

 wib.Activate wib.Range("C2").Select wib.AutoFilterMode = False Debug.Print "Bla.." wib.ListObjects("Table1").Range.AutoFilter Field:=3, Criteria1:= _ manName MsgBox "Table Filtered" 

我在即时窗口得到这个

布拉..

布拉..

我从来没有得到Msgbox提示,因为错误发生在它之前的行。 但是当我检查工作表上的表格时,它被过滤了。

UserForm控件事件不受Application.EnableEvents属性的影响

所以你必须妥善处理

如果你想要控制一个UserForm事件,你可能需要添加一个模块级别的variables(比如EnableEvents ),并将其设置为TrueFalse ,以防止一些多个事件触发

例如

 Option Explicit Dim EnableEvents As Boolean '<~~ declare a UserForm scoped variable to account for its event handling ' -> need to add a check in every event handler sub you want to control with it Private Sub UserForm_Initialize() EnableEvents = True '<~~ initialize UserForm event handling variable to True End Sub Private Sub ComboBox1_Change() Dim wt As Worksheet Dim wib As Worksheet Dim i As Long, j As Long, frow As Long, ck As Boolean, scol As Long, ecol As Long Dim manName As String If EnableEvents = True Then '<~~ let it "fire" the first time Application.EnableEvents = False '<~~ this may not be necessary anymore manName = Me.ComboBox1.Value Set wt = Sheet3 Set wib = Sheet9 'IB Skills Sheet wt.Activate wt.Range("A1") = 2 If Trim(manName) = "" Then MsgBox "Manager Selected is Invalid" Exit Sub End If wib.Activate wib.Range("C2").Select wib.AutoFilterMode = False EnableEvents = False '<~~ set UserForm event handling variable to False so as not to have this event fired again till next setting it to True wib.ListObjects("Tabella1").Range.AutoFilter Field:=3, Criteria1:=manName EnableEvents = True '<~~ set UserForm event handling variable back to True to have this event fired normally Application.EnableEvents = True ''<~~ this may not be necessary anymore End If End Sub 

编辑 :添加了本地事件控制模式

如果您只想处理单个控件特定事件,则可能需要使用如下所示的staticvariables

 Option Explicit Private Sub ComboBox1_Change() Dim wt As Worksheet Dim wib As Worksheet Dim i As Long, j As Long, frow As Long, ck As Boolean, scol As Long, ecol As Long Dim manName As String Static ComboBox1_Change As Boolean '<~~ "event-level" variable controlling its triggering ComboBox1_Change = Not ComboBox1_Change '<~~ toggle the "event-level" variable -> this will have it fire alternatively (you may want to set a different behavior) If ComboBox1_Change = True Then '<~~ it'll fire alternatively: 1st time YES, 2nd time NO, 3rd time YES... ... here goes your code ... End If End Sub