Results 1 to 9 of 9

Thread: Remove Duplicates From A Listbox?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Remove Duplicates From A Listbox?

    Hi,

    Here's an example listbox:
    Hello World
    Hi World
    Hello World
    How would I go about removing just one of the "Hello World" entries?

    And this also needs to work with multiple duplicated text and without me specifying the duplicates.

    Basically I need some code to check through a long listbox, find duplicates and remove them. (if there's 2 duplicates, remove 1, if theres 3 duplicates, remove 2 etc. etc.) I hope I've explained that enough for you to understand.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Remove Duplicates From A Listbox?

    Code:
    Private Sub Command1_Click()
    Dim i As Long
    Dim j As Long
        With List1
            For i = 0 To .ListCount - 1
                For j = .ListCount To (i + 1) Step -1
                    If .List(j) = .List(i) Then
                        .RemoveItem j
                    End If
                Next
            Next
        End With
    
    End Sub

  3. #3

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Remove Duplicates From A Listbox?

    The best logic is to prevent the duplicates before they get added to the listbox.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Remove Duplicates From A Listbox?

    Quote Originally Posted by RobDog888
    The best logic is to prevent the duplicates before they get added to the listbox.
    Agreed. And, along those lines, this should do the trick.
    Code:
    Option Explicit
    
    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 AddToListbox(strListBItem As String, LB As ListBox)
    
        Dim lngListIndex As Long
        
        ' lngListIndex is the ListIndex if the item is found
        lngListIndex = SendMessage(LB.hwnd, LB_FINDSTRINGEXACT, -1, ByVal strListBItem)
        If lngListIndex = -1 Then
            LB.AddItem strListBItem
        Else
            MsgBox strListBItem & " is already in the Listbox"
            Exit Sub
        End If
    End Sub
    
    Private Sub Command1_Click()
    AddToListBox Text1.Text, List1
    End Sub

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Remove Duplicates From A Listbox?

    No thats not what I meant. If you are populating the listbox with duplicates then change your logic/code to filter and prevent the source from containing any duplictes at all. Like if its a query you can use the SELECT DISTINCT function in your sql depending upon the complexity of your query it will be as simply as that.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Remove Duplicates From A Listbox?

    vb Code:
    1. Public Sub DeDupeList(ByVal List As ListBox)
    2.     On Error Resume Next
    3.     Dim lstCollection As New Collection
    4.     Dim i As Long
    5.         For i = 0 To List.ListCount - 1
    6.             lstCollection.Add List.List(i), List.List(i)
    7.         Next i
    8.         List.Clear
    9.         For i = 1 To lstCollection.Count
    10.             List.AddItem lstCollection.Item(i)
    11.         Next i
    12. End Sub

    Man am I gonna hear it for using On Error Resume Next

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Remove Duplicates From A Listbox?

    Dont use "On Error Resume Next! Its bad programming practice!!!"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Member
    Join Date
    Jul 2008
    Posts
    36

    Re: Remove Duplicates From A Listbox?

    Or just use Error Handling for "speed"...Collections are good...if you add a duplicate item (key) to a collection, you get an error.

    But, for looping through it, Hack's method using API is the fastest I know of...

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