vba listbox事件触发两次

我正试图让这个在excel用户表单中工作。 在列表框中select一个值时,它的变化事件被调用两次。 即使放置了旗帜,也无法绕过它。 不知道为什么更改事件被调用两次。 谷歌search之后,像控制得到焦点的变化事件被称为接缝。 下面是代码。

Public eventsOFF As Boolean Public ctr As Integer Private Sub ListBox1_Change() Dim tmp As String, sel As Variant, s As Variant If eventsOFF Then Exit Sub eventsOFF = True For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then tmp = tmp & ListBox1.List(i) & "," Next ListBox1.Clear sel = Split(tmp, ",") ListBox1.AddItem "Entry 1" ListBox1.AddItem "Entry 2" ListBox1.AddItem "Entry 3" ListBox1.AddItem "Entry 4" ListBox1.AddItem "Entry 5" For i = 0 To ListBox1.ListCount - 1 For Each s In sel If s = ListBox1.List(i) Then ListBox1.Selected(i) = True Next Next eventsOFF = False ctr = ctr + 1 Debug.Print ctr End Sub Private Sub UserForm_Initialize() ListBox1.AddItem "Entry 1" ListBox1.AddItem "Entry 2" ListBox1.AddItem "Entry 3" ListBox1.AddItem "Entry 4" ListBox1.AddItem "Entry 5" End Sub 

在ListBox1_MouseMove事件下插入以下行将解决您的问题

ListBox1.SetFocus