这个抓住了我 - 我有一个有三个组合框的表格,它们是:
Umbrella_Dropdown:
Umbrella_Dropdown_Change触发器不会更改其他两个组合框中的值,但会更改其中列出的项目(因此它不会触发其他两个中的_change
事件(不要担心声明 - 它们'在全球范围宣布):
Private Sub Umbrella_Dropdown_Change()
If Fund_Temination.Umbrella_Dropdown.ListIndex = -1 And Fund_Temination.Umbrella_Dropdown.Value <> "" Then
MsgBox "Please select from drop-down", vbInformation, "Select from drop-down"
Fund_Temination.Umbrella_Dropdown.Value = ""
Exit Sub
End If
sString = Umbrella_Dropdown.Value
l = 2
Fund_Temination.Fund_Name_Dropdown.Clear
Fund_Temination.MCH_Dropdown.Clear
Do Until l > WorksheetFunction.Max(Sheets("List of current funds").Range("C1048576").End(xlUp).Row, Sheets("List of current funds").Range("A1048576").End(xlUp).Row)
If sString = "" Then
If Trim(Sheets("List of current funds").Range("C" & l).Value) <> "" And Trim(Sheets("List of current funds").Range("A" & l).Value) <> "" Then
Fund_Temination.Fund_Name_Dropdown.AddItem Sheets("List of current funds").Range("C" & l).Value
Fund_Temination.MCH_Dropdown.AddItem Sheets("List of current funds").Range("A" & l).Value
End If
Else
If Trim(Sheets("List of current funds").Range("C" & l).Value) <> "" And Trim(Sheets("List of current funds").Range("B" & l).Value) = sString And Trim(Sheets("List of current funds").Range("A" & l).Value) <> "" Then
Fund_Temination.Fund_Name_Dropdown.AddItem Sheets("List of current funds").Range("C" & l).Value
Fund_Temination.MCH_Dropdown.AddItem Sheets("List of current funds").Range("A" & l).Value
End If
End If
l = l + 1
Loop
sString = ""
End Sub
MCH_Dropdown: MCH_Dropdown_Change触发器确实更改了其他两个组合框的值,虽然我在更改值之前已经包含Application.enableEvents = False
但它似乎没有技巧并触发Umbrella_Dropdown_Change和Fund_Name_Dropdown_Change事件:
Private Sub MCH_Dropdown_Change()
If Len(Fund_Temination.MCH_Dropdown.Value) = 4 Then
If Not Fund_Temination.MCH_Dropdown.ListIndex = -1 Then
l = 1
Do Until l > WorksheetFunction.Max(Sheets("List of current funds").Range("C1048576").End(xlUp).Row, Sheets("List of current funds").Range("A1048576").End(xlUp).Row)
If Sheets("List of current funds").Range("A" & l).Value = Fund_Temination.MCH_Dropdown.Value Then
Application.EnableEvents = False
Fund_Temination.Fund_Name_Dropdown.Value = Sheets("List of current funds").Range("C" & l).Value
Fund_Temination.Umbrella_Dropdown.Value = Sheets("List of current funds").Range("B" & l).Value
Application.EnableEvents = True
GoTo Finish
End If
l = l + 1
Loop
Else
MsgBox "The MCH entered is not listed in the dropdown" & vbNewLine & vbNewLine & "Please re-enter the correct MCH or select from the dropdown", vbCritical, "Wrong MCH code entered"
Fund_Temination.MCH_Dropdown.Value = ""
End If
End If
Finish:
End Sub
对于触发其他人的Fund_Name_Dropdown_Change事件也可以这么说(虽然我只是通过我的代码,我可以看到这会导致无限的“循环”,其中Fund_Name_Dropdown_Change触发MCH_Dropdown_Change,这会触发Fund_Name_Dropdown_Change等等在时间的尽头..)
无论如何,如果你想看到Fund_Name_Dropdown_Change事件的代码就是这样:
Private Sub Fund_Name_Dropdown_Change()
If Not Fund_Temination.Fund_Name_Dropdown.ListIndex = -1 Then
l = 1
Do Until l > WorksheetFunction.Max(Sheets("List of current funds").Range("C1048576").End(xlUp).Row, Sheets("List of current funds").Range("A1048576").End(xlUp).Row)
If Sheets("List of current funds").Range("C" & l).Value = Fund_Temination.Fund_Name_Dropdown.Value Then
Application.EnableEvents = False
Fund_Temination.MCH_Dropdown.Value = Sheets("List of current funds").Range("A" & l).Value
Fund_Temination.Umbrella_Dropdown.Value = Sheets("List of current funds").Range("B" & l).Value
Application.EnableEvents = True
GoTo Finish
End If
l = l + 1
Loop
End If
Finish:
End Sub
我可以做些什么来关闭触发器?! 如果您需要任何进一步的信息,请告诉我,但我想我已经列出了一切。
提前致谢
答案 0 :(得分:1)
Application.EnableEvents
主要对UserForm对象没有影响。 (只是对冲我的陈述,所以人们不会开始给我举例)
您需要的是表单范围变量(最好的方法是添加自定义属性)来存储和操作表单的事件状态。
请参阅此示例并按照以下步骤重新编写代码:
'/ UserForm with 2 CheckBoxes : CheckBox1 and CheckBox2
Private m_bEvents As Boolean
Public Property Let EnableFormEvents(bVal As Boolean)
m_bEvents = bVal
End Property
Public Property Get EnableFormEvents() As Boolean
EnableFormEvents = m_bEvents
End Property
Private Sub CheckBox1_Click()
'/ Custom Event Status
Me.EnableFormEvents = False
Me.CheckBox2 = Me.CheckBox1
Me.EnableFormEvents = True
End Sub
Private Sub CheckBox2_Click()
If Me.EnableFormEvents Then
MsgBox "Check box clicked by user."
Else
MsgBox "Check box clicked by code."
End If
End Sub