Results 1 to 2 of 2

Thread: Removing doubles in a list

  1. #1


    I wrote some code to remove repeated items in a list, but the only way I could get it to work is very slow and ineffective for very large lists. Please let me know how I can make this one work better. BTW the variables X and Y were dimmed already in Generals

    For X = 0 To frmMain.lstURLS.ListCount - 1
    For Y = 0 To frmMain.lstURLS.ListCount - 1
    If frmMain.lstURLS.List(X) = frmMain.lstURLS.List(Y) And X <> Y Then
    frmMain.lstURLS.RemoveItem (Y)
    GoTo Restart:
    End If
    Next Y
    Next X

    Thanks for taking a look.

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Red Wing, MN, USA


    Try this:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const LB_FINDSTRINGEXACT = &H1A2
    Private Sub Command1_Click()
        Dim iIndex As Long
        Dim iCur As Long
        For iCur = 1 To List1.ListCount
            While iIndex > -1 And iIndex <> iCur
                iIndex = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, iCur, ByVal List1.List(iCur))
                If iIndex <> iCur And iIndex > -1 Then
                    List1.RemoveItem iIndex
                End If
    End Sub
    N.B. It would work even better and faster if you checked for repeat occurances before adding an Item to the List, then you'd only need to look for 1 Repeat Occurance.

    Aaron Young
    Analyst Programmer

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