Remove Item Fires Click Event

St Jimmy

New Member
Joined
Oct 29, 2015
Messages
36
I would like to remove an item from a ListBox in VBA by simply clicking the list item.
I have an event that handles this on click, but when I call the .RemoveItem method it automatically fires the click event again.
Excel Formula:
Private Sub RequestedList_Click()
    RequestedList.RemoveItem RequestedList.ListIndex
End Sub

I have tried to set Application.EnableEvents = False but it didn't work (because I was in a form?)

Any ideas?

Thank you!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
First, declare the following module-level Boolean variable (declare it at the very top of your UserForm code module)...

VBA Code:
Dim m_locked As Boolean

Then amend your existing code as follows...

VBA Code:
Private Sub RequestedList_Click()

    If m_locked = True Then Exit Sub
 
    m_locked = True
    RequestedList.RemoveItem RequestedList.ListIndex
    m_locked = False
 
End Sub

Note, though, when you click on an item that has focus, the click event does not get triggered. Maybe the DblClick event would be better?


Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,180
Messages
6,129,339
Members
449,504
Latest member
Alan the procrastinator

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top