Events Excel VBA-更新组合框时暂停事件

Events Excel VBA-更新组合框时暂停事件,events,excel,userform,vba,Events,Excel,Userform,Vba,我有一个用VBA for Excel编写的应用程序,它接收实时数据提要。每当数据发生变化时,VBA中就会触发各种事件 我还有一些带有组合框的用户表单。我的问题是,当我单击组合框上的向下箭头并尝试进行选择时,当我从数据提要获得更新时,组合框将重置。我想做的是在组合框中进行选择时暂停事件,然后在完成选择后取消暂停。如何生成此功能?请尝试关闭: application.enableevents=false 这是要重新打开的: application.enableevents=true 请尝试此选项以关

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

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

请尝试关闭:

application.enableevents=false

这是要重新打开的:

application.enableevents=true

请尝试此选项以关闭:

application.enableevents=false

这是要重新打开的:

application.enableevents=true


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

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
Private块作为布尔值在模块级声明
'当用户单击组合框时
专用子下拉箭头\u Click()'或cboComboBox\u Click()
阻塞=真
端接头
'此过程是从数据源执行更新的过程。
'如果设置了标志,请勿触摸组合框。
私有子更新mboboxes()
如果没有封锁,那么
'更新组合框
如果结束
端接头
'进行选择或组合框中的焦点更改时。
'检查是否进行了选择并重置标志。
私有子cboComboBox_AfterUpdate()'或LostFucus或其他内容
如果格式(cboComboBox.Value)为vbNullString,则
阻塞=错误
如果结束
端接头

希望对您有所帮助。

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

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
Private块作为布尔值在模块级声明
'当用户单击组合框时
专用子下拉箭头\u Click()'或cboComboBox\u Click()
阻塞=真
端接头
'此过程是从数据源执行更新的过程。
'如果设置了标志,请勿触摸组合框。
私有子更新mboboxes()
如果没有封锁,那么
'更新组合框
如果结束
端接头
'进行选择或组合框中的焦点更改时。
'检查是否进行了选择并重置标志。
私有子cboComboBox_AfterUpdate()'或LostFucus或其他内容
如果格式(cboComboBox.Value)为vbNullString,则
阻塞=错误
如果结束
端接头

希望这会有所帮助。

暂停并显示消息,并在暂停期间继续进行某些工作。最后按下按钮

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

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

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

因此,我假设我会在ComboBox中的DropDownArrow过程中将Application.EnableEvents设置为false,但我不希望在进行选择之前重新启用这些事件。这有助于查看一些代码以真正了解发生了什么。真正的问题是为什么组合框会重置?你能发布代码吗?我找到了问题所在。我编写了一个函数来更新userform。显然,该函数还重置了组合框。通过限制对函数的调用,我能够解决这个问题。感谢您为我指明了正确的方向。因此,我假设我会在ComboBox中的DropDownArrow过程中将Application.EnableEvents设置为false,但我不希望在进行选择之前重新启用这些事件。这有助于查看一些代码以真正了解发生了什么。真正的问题是为什么组合框会重置?你能发布代码吗?我找到了问题所在。我编写了一个函数来更新userform。显然,该函数还重置了组合框。通过限制对函数的调用,我能够解决这个问题。谢谢你给我指出了正确的方向。我能够找出是什么导致组合框重置,但我肯定喜欢你的解决方案,因为点击向下箭头实际上会暂停更新。谢谢你的建议。我能够找出是什么导致组合框重置,但我肯定喜欢你的解决方案,因为点击向下箭头实际上会暂停更新。谢谢你的建议。