我正在尝试这样做,它运行正常,我根据条件筛选了表格,但它给了我错误:自动筛选范围类方法失败。
我不确定我是否使用正确的事件来触发此代码:
以下是最后一行给出错误的代码的一部分。
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"
我在即时窗口中看到了这个
布拉..
Bla ..
我从未收到Msgbox提示,因为错误发生在它之前的行上。但是,当我检查工作表上的表时,它会被过滤。
答案 0 :(得分:2)
UserForm
控件事件不受Application.EnableEvents
属性
这样你就必须妥善处理它们
是否要控制单个UserForm事件,您可能希望添加模块级变量(例如EnableEvents
)并按顺序将其设置为True
或False
防止一些多事件触发
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
修改:添加了本地事件控制模式
如果您只想处理单个控件特定事件,则可能需要使用static
变量,如下所示
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