AnsweredAssumed Answered

VBA - Remove (or prevent adding) duplicates from/to a listbox.

Question asked by Cad Admin on Mar 14, 2019
Latest reply on Mar 14, 2019 by Cad Admin

General VB, i have a single column, multi-select listbox i am populating with file paths from components in an assembly.  I need to add the path ONLY if it doesn't already exist.


i can either do it as i process the components of the assembly, or after the fact.  I would prefer while.  that being said the function below is giving me an error. of: "vba could not get the list property. invalid property array index"


Private Function AddListBoxItem(pathName As String) As Boolean

    Dim x As Integer

    For x = 0 To F1.Lb.ListCount

        If LCase(pathName) = LCase(F1.Lb.List(x)) Then

            'It Exists

            F1.Lb.List(x) = pathName

            AddListBoxItem = False

            Exit For


            AddListBoxItem = True

        End If

    Next x


    If AddListBoxItem = True Then

       F1.Lb.AddItem pathName


       MsgBox "Item already exists"

    End If

End Function


i thought (via a google searches) that using "contains" as below was supposed to compare, but contains was not avail.