Consulting

Results 1 to 4 of 4

Thread: Listbox navigation

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Listbox navigation

    I have a listbox open a userform and there are two things i would like to be able to do

    the first I am sure is quite easy (When you know how) and thats to add a previous and next button to my user form to select the item in the list box

    the second is to be able to type into a text box that will search the list box for a match.

    Thanks in advance

    Gibbo

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this code.

    Option Explicit
      
      Private Sub CommandButton1_Click()  'Previous
    Dim n               As Long
    n = Me.ListBox1.ListCount - 1
          Select Case Me.ListBox1.ListIndex
              Case Is < 1
                  Me.ListBox1.ListIndex = n
              Case Else
                  Me.ListBox1.ListIndex = Me.ListBox1.ListIndex - 1
          End Select
    End Sub
      
      Private Sub CommandButton2_Click()  'Next
    Dim n               As Long
    n = Me.ListBox1.ListCount - 1
          Select Case Me.ListBox1.ListIndex
              Case Is < n
                  Me.ListBox1.ListIndex = Me.ListBox1.ListIndex + 1
              Case Else
                  Me.ListBox1.ListIndex = 0
          End Select
    End Sub
      
      Private Sub CommandButton3_Click()  'Search
    Dim Search          As String
      Dim n               As Long
      Dim i               As Long
      Dim j               As Long
    Search = Me.TextBox1.Text
          n = Len(Me.TextBox1)
          j = Me.ListBox1.ListCount - 1
          For i = 0 To j
              If Left(Me.ListBox1.List(i), n) = Search Then
                  Me.ListBox1.Selected(i) = True
                  Exit For
              End If
          Next
    End Sub

  3. #3
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Jake i cant thank you enough,

    this works great

    Gibbo

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Glad to help

    Take Care

Posting Permissions

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