Last Item in ListBox Causes All Item to be Removed

sdday81

New Member
Joined
Feb 28, 2014
Messages
25
For some reason the code below will end up deleting every item in the listbox when you select the last item in the list. If I select any other item besides the last item in the list it will only delete that selected item.

Code:
Private Sub DeleteIngredientButton_Click()

    Dim Index As Integer
    
    'Delete the selected items from the list of ingredients
    For Index = IngredientList.ListCount - 1 To 0 Step -1
        If IngredientList.Selected(Index) Then
             IngredientList.RemoveItem (Index)
        End If
    Next Index
    
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Add

Code:
IngredientList.ListIndex = -1

after your line

Code:
IngredientList.RemoveItem (Index)
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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