# Remove items from listbox in for next loop

#### selant

##### Board Regular
I want to remove the items, if it meets the criteria. Listbox contains about 25000 items

Code:
aset = ListBox2.ListCount

For i = 0 To aset

check1 = Split(ListBox2.List(asd), ",")
check2 = Split(check1(0), ".")

If (CStr(check2(1)) <> "02" Then
ListBox2.Selected(asd) = True
[COLOR=#ff0000]    ??? ListBox2.RemoveItem ????    [/COLOR]
End If

Next i

### Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
would you not be better performing this on the actual list where ever you have it,

Yes its better but i dont know how to filter the source data from worksheet to my listbox. If its faster and easier i would prefer

I want to remove the items, if it meets the criteria. Listbox contains about 25000 items

Code:
aset = ListBox2.ListCount

For i = 0 To aset

check1 = Split(ListBox2.List(asd), ",")
check2 = Split(check1(0), ".")

If (CStr(check2(1)) <> "02" Then
ListBox2.Selected(asd) = True
[COLOR=#ff0000]    ??? ListBox2.RemoveItem ????    [/COLOR]
End If

Next i

there is an error in syntax, "asd" is "(i)" for all parameters

To remove from the listbox it's simply
Code:
ListBox2.RemoveItem i
but you need to loop backwards
Code:
For i = aset - 1 to 0 step -1

RoryA it did the work, thank you. What is the reason to to loop backwards, what is the point i cant figure out ?

Imagine you have three items. Your loop is from 1 to 3 (it's actually 0 to 2 but I'm simplifying things ).
If you remove an item on the second loop, you now have two items but your loop variable increments to 3 and then tries to access item 3 which no longer exists (it's now item 2). Not only do you get an error, but item 3 is never actually processed.
If you loop backwards, when you delete item 2, item 3 (which you have already processed) moves to item 2 as before but your counter then moves to 1 so there is no problem.

Imagine you have three items. Your loop is from 1 to 3 (it's actually 0 to 2 but I'm simplifying things ).
If you remove an item on the second loop, you now have two items but your loop variable increments to 3 and then tries to access item 3 which no longer exists (it's now item 2). Not only do you get an error, but item 3 is never actually processed.
If you loop backwards, when you delete item 2, item 3 (which you have already processed) moves to item 2 as before but your counter then moves to 1 so there is no problem.

i was facing this problem, because when i removed the item, the loop was increasing but i didnt figured out myself to make it backwards, this is a very nice explanation to understand. thank you again.

Replies
1
Views
115
Replies
6
Views
169
Replies
2
Views
170
Replies
0
Views
415
Replies
1
Views
590

1,196,409
Messages
6,015,106
Members
441,870
Latest member
kojack

### 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.

### Which adblocker are you using?

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

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