繁体   English   中英

Excel VBA-更新ComboBox时暂停事件

[英]Excel VBA - Pause Events while Updating ComboBox

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

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

尝试关闭此功能:

application.enableevents = false

然后重新打开:

application.enableevents = true

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

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

也许您可以在组合框上放置一个标记以绕过更新事件,直到做出选择为止。

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

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM