help: removing duplicative items in listbox

J

jasonsweeney

I have a listbox in a userform in which a large list of names is place
(using .additem).

What I want to do is this: After all the names are added into th
listbox, I want to remove any duplicative entries. I.e. if "Joh
Brown" is entered 3 times in the list, I want 2 of those entrie
removed from the list.

My guess is that I am making this much more painful than it has to be
but here is what I have (and it does not work):

__________________
Private Sub removeduplicates()

mainindex = 0
indexnumber = 0
totalcount = UserForm10.ListBox1.ListCount


For m = 0 To totalcount
UserForm10.ListBox1.ListIndex = mainindex
teststring = UserForm10.ListBox1.Value
totalcount = UserForm10.ListBox1.ListCount
UserForm10.ListBox1.ListIndex = indexnumber
MsgBox "comparing: " & teststring & " with: " & ListBox1.Value
If teststring = UserForm10.ListBox1.Value Then
MsgBox "Name already here: " & teststring
UserForm10.ListBox1.RemoveItem (indexnumber)
UserForm10.ListBox1.AddItem (teststring)
indexnumber = 0
End If
indexnumber = indexnumber + 1
If indexnumber >= totalcount Then
MsgBox "now finishing " & teststring
mainindex = mainindex + 1
indexnumber = 0
If mainindex >= totalcount Then
MsgBox "exiting sub"
Exit Sub
End If
MsgBox "total count is: " & totalcount
End If
Nex
 
P

peter

Hi,
Buy the book, Excel 2002 Power Programming with VBA by
John Walkenbach, page 424. I don't hink I'm allowed to
reprint his code. But if you get an answer from someone
else, the book is still worth it.

peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top