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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Add

Code:
IngredientList.ListIndex = -1

after your line

Code:
IngredientList.RemoveItem (Index)
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,566
Members
449,171
Latest member
jominadeo

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