Remove items from listbox in for next loop

selant

Board Regular
Joined
Mar 26, 2009
Messages
109
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
would you not be better performing this on the actual list where ever you have it,
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
RoryA it did the work, thank you. What is the reason to to loop backwards, what is the point i cant figure out ?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

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