how do i remove duplicates in a listbox??? please give code ---
THANK YOU THANK YOU THANK YOU!!!!!
Printable View
how do i remove duplicates in a listbox??? please give code ---
THANK YOU THANK YOU THANK YOU!!!!!
It is a base arithmetic.
How do you programming?????????????
on a form create a listbox named list1 and put the following code in a commandbox
^^^ this code will look for matches by caseCode:on error resume next
for n = 0 to list1.listcount - 1 'loop the listbox
cur = list1.list(n) 'sets the current list item
for m = 0 to list1.listcount - 1 'now loop for the victims
if m <> n and list1.list(m) = cur then 'we found a match!
list1.removeitem m 'now remove it
'add more code here to execute when finding a match
end if
next m
next n
example: 'abc' will match with 'abc' but not 'ABC'
if you do not want it case sensitive.. modify the code above with this line:
^^^now it will look for matches by similarityCode:if m <> n and ucase$(list1.list(m)) = ucase$(cur) then
example: 'abc' will match with 'abc' and 'ABC'
Hi
2 things
1) Any time u remove items from a list box you should always loop backwards. In the prev code from da404 u should change loops to ....
VB Code:
on error resume next [b]for n = list1.listcount - 1 to 0 Step - 1 [/b] 'loop the listbox cur = list1.list(n) 'sets the current list item [b]for m = list1.listcount - 1 to 0 Step -1 [/b] 'now loop for the victims if m <> n and list1.list(m) = cur then 'we found a match! list1.removeitem m 'now remove it 'add more code here to execute when finding a match end if next m next n
2) Secondly, that code will be ok for small amounts of items but if u want to remove duplicates from a large list then it is much quicker to sort the list first and then loop thru it only once removing duplicates
Regards
Stuart
beachbum: Nice little routine. I have something that I did to do this, but your routine is much, much tighter.
Hi Hack
I didnt write that routine... only modified it to work. It was da404's.
regards
Stuart