我有一个用 VBA for Excel 编写的应用程序,它接收实时数据馈送。每当数据发生变化时,都会在 VBA 中触发各种事件。
我也有一些带有组合框的用户窗体。我的问题是,当我单击组合框上的向下箭头并尝试进行选择时,当我从数据馈送中获得更新时,组合框会重置。我想做的是在 ComboBox 中进行选择时暂停事件,然后在完成后取消暂停。如何生成此功能?
试试这个来关闭:
应用程序.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
希望有帮助。