Results 1 to 6 of 6

Thread: Lists and Getting Rid of Multiple Items

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    146

    Lists and Getting Rid of Multiple Items

    Hi,

    Is there any way to remove multiple items for a list. For example, the list:

    A
    H
    A
    A
    K

    Is there any way I could just turn it into:

    A
    H
    K

    ...and remove the extra A's. Thanks.

  2. #2

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Here's a quick and dirty function to remove duplicates from a listbox (I grabbed it from someone on this forum a while back, my apologies, I can't remember who).

    VB Code:
    1. ' ideally, put this in a module (but it can go directly in your form)
    2. Public Sub RemoveDups(ListName As ListBox)
    3.  
    4.     'REMOVE DUPLICATES FROM LISTBOX
    5.    
    6.     Dim i As Integer
    7.     Dim j As Integer
    8.  
    9.  
    10.     With ListName
    11.         For i = 0 To .ListCount - 1
    12.             For j = .ListCount To (i + 1) Step -1
    13.            
    14.                 If .List(j) = .List(i) Then
    15.                     .RemoveItem j
    16.                 End If
    17.                
    18.             Next
    19.         Next
    20.     End With
    21.  
    22. End Sub

    Usage:
    VB Code:
    1. RemoveDups List1
    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    i like this one, its really fast.
    VB Code:
    1. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    2. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    3. Const LB_FINDSTRINGEXACT = &H1A2
    4.  
    5.  
    6. Dim i As Integer
    7.  
    8. For i = List1.ListCount - 1 To 0 Step -1
    9.  If SendMessageString(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal List1.List(i)) <> i Then
    10.   List1.RemoveItem i
    11.  End If
    12. Next i

    casey.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    146
    How would I change that code to make it remove every instance of the dupe? For example:

    A
    A
    B
    C

    Should be changed to:

    B
    C

    Thanks.

  6. #6

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