Excel VBA - 更新ComboBox时暂停事件

时间:2010-01-12 21:38:03

标签: events excel-vba userform vba excel

我有一个用VBA for Excel编写的应用程序,它接收实时数据源。只要数据发生变化,就会在VBA内触发各种事件。

我还有一些带有ComboBoxes的UserForms。我的问题是,当我单击ComboBox上的向下箭头并尝试进行选择时,当我从数据源获得更新时,ComboBox将重置。我想做的是在我在ComboBox中进行选择时暂停事件,然后在完成后取消暂停。如何生成此功能?

3 个答案:

答案 0 :(得分:2)

请尝试关闭此功能:

  

application.enableevents = false

这将重新开启:

  

application.enableevents = true

答案 1 :(得分:2)

暂停并显示消息并在暂停期间继续处理某些事情。最后按下按钮

Public Ready As Boolean

Private Sub Command1_Click()
Ready = True
End Sub

Private Sub Form_Load()
Me.Show
Ready = False
Call Wait
Label1.Visible = True
End Sub

Public Function Wait()
Do While Ready = False
    DoEvents
Loop
End Function

答案 2 :(得分:1)

也许你可以在你的组合框上放置一个标志来绕过更新事件,直到做出选择。

Private bLock as boolean  ' declare at module level

' When a user clicks on the combobox
Private Sub DropDownArrow_Click()  ' or cboComboBox_Click()
    bLocked = True
End Sub

' This procedure is the one that does the updating from the data source.
' If the flag is set, do not touch the comboboxes.
Private Sub subUpdateComboBoxes()
    If Not bLocked then
        ' Update the comboboxes
    End If
End Sub


' When the selection is made, or the focus changes from the combobox.
' check if a selection is made and reset the flag.
Private Sub cboComboBox_AfterUpdate()  ' Or LostFucus or something else
    if Format(cboComboBox.Value) <> vbNullString Then
        bLocked = False
    End If
End Sub

希望有所帮助。