-
Here's the code:
Code:
Sub remove_dupe_list(lst As ListBox)
For a = 0 To (lst.ListCount - 1)
X$ = LCase(lst.List(a))
For b = 0 To (lst.ListCount - 1)
Y$ = LCase(lst.List(b))
If a <> b And X$ Like Y$ Then lst.RemoveItem b
Next b
Next a
End Sub
But when I use it, my program freezes. Anyone have a better code or can fix it?
-
Why exactly are you using the 'Like' operator. I'm assuming you want to remove all of the dups in the list. You are already making the comparision with lower case so you should just use the '=' operator. Also, try placing a 'DoEvents' after your item remove.
-
Also, you can use the 'LCase$()' function instead of the 'LCase()' function. It is much faster.
-
Did everything you said..nothing worked. I even put a timout .01 in there and it still freezes.
-
Step through it with the debugger. What you are trying to do is risky cuz you are resizing an array in a loop that depends on the array size. It's pretty bad coding practice to do this. What you might want to try and do is build a temp list of all bins you want to remove from the list. Then in another loop call your remove on those specfic list items.
-
You know what I don't understand? The removing duplicates from a list works with a combo box but not a list box. Wonder why that is?
-
uh.. I remove dupes in list boxes all the time.. And I do it pretty much like you do and it works fine:
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
For i = 0 To List1.ListCount - 1
If i = List1.ListCount Then Exit Sub
For j = 0 To List1.ListCount - 1
If j <> List1.ListCount Then
If LCase$(List1.List(i)) = LCase$ _
(List1.List(j)) And i <> j Then
List1.RemoveItem (j)
j = 0
End If
End If
Next j
Next i
End Sub
That should do you... you don't need DoEvents in there unless you're dupe checking big lists.
-Reprise
-
Dim x As Single
Dim y As Single
Dim z As Single
z = 1
For x = 0 To List1.ListCount - 1
For y = z To List1.ListCount - 1
If List1.List(x) = List1.List(y) Then List1.RemoveItem y
Next y
z = z + 1
Next x
This'll remove any duplicates you might have.....if you want to remove both of them from the list, add :List1.RemoveItem x, after List1.RemoveItem y
Hope that helps...
-
Uh.. Darknight, your code misses duplicates in the last two positions of the listbox.
-Reprise