Consulting

Results 1 to 2 of 2

Thread: Unique items in ListBox

  1. #1

    Unique items in ListBox

    Hi guys

    I'm trying to add items to a ListBox through a TextBox on condition that the items in the ListBox are unique ..
    In other words,If I tried to add a new item through the textbox a message appeared to tell me that "The item is listed in the listbox"

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Perhaps
    [VBA]Private Sub CommandButton1_Click()
    Dim newItem As String
    Dim listPointer As Long

    newItem = TextBox1.Text

    With ListBox1
    For listPointer = 0 To .ListCount - 1
    If LCase(newItem) = LCase(.List(listPointer)) Then
    MsgBox newItem & " is already in the list."
    Exit Sub
    ElseIf LCase(newItem) < LCase(.List(listPointer)) Then
    Exit For
    End If
    Next listPointer

    .AddItem newItem, listPointer
    TextBox1.Text = vbNullString
    End With
    End Sub[/VBA]

Posting Permissions

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