| Prevent Duplicate Values from Being Added to a List Control Public Sub AddUnique(lst As Object, str As String) 'Purpose: Add String to a list control 'only if that string is not already there 'Parameters: lst: ListBox, ComboBox, or 'any control with a similar interface 'str: string value to add if 'that string is not already there Dim nIndex As Integer Dim bFound As Boolean Dim vTest As Variant On Error Resume Next 'this test allows you 'to use the function for 'any control that supports 'listbox like functionality vTest = lst.ListCount If vTest > 0 Then vTest = lst.List(0) vTest = lst.ListIndex lst.AddItem vTest lst.Clear If Err.Number > 0 Then Exit Sub On Error GoTo 0 ' Add a string to a list box only if it does not ' already exist With lst For nIndex = 0 To .ListCount - 1 If .List(nIndex) = str Then bFound = True Exit For End If Next If Not bFound Then .AddItem str End If End With End Sub |
Prevent Duplicate Values from Being Added to a Lis |
Express News India | Freelance ecommerce web development India