无法停止Combobox_Change事件VBA触发另一个Combobox_Change事件

这一个引起了我的注意 – 我有一个三个combobox的forms,它们是:

在这里输入图像说明

Umbrella_Dropdown: Umbrella_Dropdown_Change触发器不会改变其他两个组合_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触发器确实改变了另外两个combobox的值,尽pipe在改变值之前我已经包含了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 

我能做些什么来closures触发器? 如果您需要更多的信息,请让我知道,但我想我已经列出了一切。

提前致谢

Application.EnableEvents大多数对用户窗体对象没有影响。 (只是对我的陈述,所以人们不会开始给我的例子)

你需要的是一个窗体范围variables(最好的方法是添加一个自定义属性)来存储和操作窗体的事件状态。

看到这个例子,沿着这个重新编写你的代码:

 '/ 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