code delete all items in list when select last item from listbox2 to

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hi

I have tried two different codes for removing items from listbox2.

It works fine when I select one of the items in the middle, but as soon as I select the last one in the list, all of the items are removed at the same time...

Code:
If ListBoxX.ListIndex = -1 Then Exit Sub

    For i = ListBoxX.ListCount - 1 To 0 Step -1

    If ListBoxX.Selected(i) = True Then
    
        Sheets("1HourHL").Range("X10").Value = Me.ListBoxX.Value
        ListBoxX.RemoveItem i
                    
    End If

    Next i

I cannot find what is causing this, but I have a feeling that it could be pretty obvious when I learn what it is...

Kind regards
Espen
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Espen

If you want to remove all the items from a listbox you can use Clear.
Code:
ListboxX.Clear
By the way, is your listbox multiselect?
 
Upvote 0
Hi Norie

The problem is that I only want to remove the one that is selected. And it works fine, except from when I select the last item in my list, then the whole list is removed.

Regards
Espen
 
Upvote 0
Espen

If you only want to remove the selected item why are you looping through all the other items?

This is all you should need.
Code:
ListboxX.RemoveItem ListboxX.ListIndex
 
Upvote 0
Good question... I'm not quite sure, I took the code from another workbook and didn't think about it...

Is it possible to have multiple selcet, with the use of Ctrl, or Shift?

Kind regards
Espen
 
Upvote 0
The problem with the first code is the selection moves up when you delete the last item.

So the last item is deleted, the 2nd from last becomes selected and is then deleted, then the 3rd from last becomes selected and then deleted and so on.
 
Upvote 0
Espen

Listboxes have a MultiSelect property you can set to allow multiple selections.
 
Upvote 0
Great, thank you for all info, that sorted things out a bit. Then I will check property of my listbox :)

Have a nice evening :)

Kind regards
Espen
 
Upvote 0

Forum statistics

Threads
1,203,453
Messages
6,055,530
Members
444,794
Latest member
HSAL

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