Results 1 to 9 of 9

Thread: Remove duplicates in a list

  1. #1
    Guest

    Question

    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?

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    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.
    -Shickadance

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Also, you can use the 'LCase$()' function instead of the 'LCase()' function. It is much faster.
    -Shickadance

  4. #4
    Guest
    Did everything you said..nothing worked. I even put a timout .01 in there and it still freezes.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    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.
    -Shickadance

  6. #6
    Guest
    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?

  7. #7
    New Member
    Join Date
    Feb 2000
    Location
    Australia
    Posts
    10
    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
    "Hello, I would like to buy a fish licence please..."

  8. #8
    Member
    Join Date
    Jul 1999
    Location
    J-ville, NC
    Posts
    54

    Lightbulb

    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...
    David Underwood
    Cannabatech Corporation

    ICQ - 14028049
    E-mail - [email protected]

    AIM - DK4ever23[/b]


  9. #9
    New Member
    Join Date
    Feb 2000
    Location
    Australia
    Posts
    10
    Uh.. Darknight, your code misses duplicates in the last two positions of the listbox.

    -Reprise
    "Hello, I would like to buy a fish licence please..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width