2016-07-15 225 views
0

这一个已经抓住了我 - 我有有三个comboboxs一种形式,它们分别是:无法停止Combobox_Change事件触发VBA另一Combobox_Change事件

enter image description here

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_Dropdo wn_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 

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

在此先感谢

回答

1

Application.EnableEvents不会对用户窗体对象的影响居多。 (只是对冲我的声明,所以人们不会给我例子)

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

见这个例子中,并沿此重新工作,你的代码:

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

我已经尝试添加上面的代码,但它不是禁用触发器 - 我把窗体的代码 – Jeremy

+0

内的代码,谢谢@ cyboamsu :) – Jeremy

+0

你可以发布更新的代码。 – cyboashu