Hello,
suppose you have a form with a ListBox on it and someone selects a different ListIndex, I want to have a MsgBox asking if they are sure they want to make the change; if Yes, then go ahead and change the ListIndex, if no, then reset back to the original ListIndex.
The problem is that when vbNo is the response, my code seems to trigger another Change event after the original call to ListBox1_Change() and the Index never gets reset.
Does anyone know how to get around this?
You can paste my example code in a userform with a listbox control to get it to work.
Thanks in advance for your help.
Cheers,
Taylour
suppose you have a form with a ListBox on it and someone selects a different ListIndex, I want to have a MsgBox asking if they are sure they want to make the change; if Yes, then go ahead and change the ListIndex, if no, then reset back to the original ListIndex.
The problem is that when vbNo is the response, my code seems to trigger another Change event after the original call to ListBox1_Change() and the Index never gets reset.
Does anyone know how to get around this?
You can paste my example code in a userform with a listbox control to get it to work.
Thanks in advance for your help.
Cheers,
Taylour
Code:
Private bEnableEvents As Boolean
Private nPreviousListIndex As Long
Private Sub ListBox1_Change()
If Not bEnableEvents Then Exit Sub
Dim nResponse As Long
nResponse = MsgBox("Are You Sure?", vbYesNo)
If nResponse = vbYes Then
nPreviousListIndex = Me.ListBox1.ListIndex
Else
bEnableEvents = False
Me.ListBox1.ListIndex = nPreviousListIndex
bEnableEvents = True
End If
End Sub
Private Sub UserForm_Initialize()
Me.ListBox1.AddItem "1"
Me.ListBox1.AddItem "2"
Me.ListBox1.AddItem "3"
Me.ListBox1.ListIndex = 1
nPreviousListIndex = Me.ListBox1.ListIndex
bEnableEvents = True
End Sub