Remove Dupes in Listbox.-VBForums
Results 1 to 6 of 6

Thread: Remove Dupes in Listbox.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Remove Dupes in Listbox.

    How do you remove the dupes in a listbox?

    I tried doing it like this

    VB Code:
    1. Dim Intx as integer
    2.  
    3. for intx = 0 to list1.listcount - 1
    4.  
    5. if list1.list(intx) = list1.text then
    6. list1.removeitem(intx)
    7. end if
    8.  
    9. next intx

    Thanks,
    Sir Loin

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Remove Dupes in Listbox.

    Nope. That will only remove what's in the textbox from the listbox. You'd be better off adding items to a second listbox, and then comparing each new item to all items in the second listbox to see if it should be deleted from the first listbox, or added to the second listbox.
    It would be easier to just build the second listbox of unique items, then copy them back into the first listbox. That way you wouldn't have to delete anything from the first listbox.

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Remove Dupes in Listbox.

    An easy way is to sort it (set Sorted to True). Then all duplicate items will be together (one after the other). Then you can use this code:

    VB Code:
    1. Dim intItems As Integer
    2. Dim I As Integer
    3.  
    4. intItems = List1.ListCount
    5.  
    6. For I = intItems To 1 Step -1
    7.      If List1.List(I) = List1.List(I-1) Then
    8.           List1.RemoveItem (I)
    9.      End If
    10. Next I

  4. #4
    Member
    Join Date
    Mar 2008
    Posts
    39

    Re: Remove Dupes in Listbox.

    Quote Originally Posted by baja_yu
    An easy way is to sort it (set Sorted to True). Then all duplicate items will be together (one after the other). Then you can use this code:

    VB Code:
    1. Dim intItems As Integer
    2. Dim I As Integer
    3.  
    4. intItems = List1.ListCount
    5.  
    6. For I = intItems To 1 Step -1
    7.      If List1.List(I) = List1.List(I-1) Then
    8.           List1.RemoveItem (I)
    9.      End If
    10. Next I
    Cleaver, Thank You!

  5. #5
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: Remove Dupes in Listbox.

    Or, if they are not sorted and together, you can do
    vb Code:
    1. Private Sub Command1_Click()
    2. Dim i As Long
    3. Dim j As Long
    4.     With List1
    5.         For i = 0 To .ListCount - 1
    6.             For j = .ListCount To (i + 1) Step -1
    7.                 If .List(j) = .List(i) Then
    8.                     .RemoveItem j
    9.                 End If
    10.             Next
    11.         Next
    12.     End With
    13. End Sub
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,469

    Re: Remove Dupes in Listbox.

    How are you adding items to the ListBox? The best method of removing duplicates is not letting them get in there in the first place...otherwise, any of the above codes work. I have always done it as Hack posted

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.