Consulting

Results 1 to 4 of 4

Thread: Goto record using listbox

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

    Goto record using listbox

    I have a dynamic range that is displayed in a list box.

    Can I select an item in the list and when i click a button close the listbox and have the active row as per my selection?

    I am using the code below re the list box

    Dim Rng As Range
     Me.ListBox1.Clear
     With ThisWorkbook.Sheets("Actions")
             Set Rng = .Range("A1", .Range("A65536").End(xlUp))
         End With
    For Each cell In Rng.Cells
             With Me.ListBox1
                 .AddItem cell.Offset(0, 2).Value
                 .List(.ListCount - 1, 1) = cell.Value
                 '.List(.ListCount - 1, 1) = cell.Offset(0, 3).Value
             End With
         Next cell
    Many thanks

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    What we can do is record the row number to a second listbox. Just set the second listbox's Visible property to False.


    Option Explicit
     
     Private Sub CommandButton1_Click()
    If Me.ListBox1.Text <> "" Then
             Range("A" & Me.ListBox2.List(Me.ListBox1.ListIndex)).Select
         End If
         Unload Me
    End Sub
     
     Private Sub UserForm_Initialize()
    Dim Rng             As Range
     Dim Cell            As Range
    Me.ListBox1.Clear
         With ThisWorkbook.Sheets("Actions")
             Set Rng = .Range("A1", .Range("A65536").End(xlUp))
         End With
         For Each Cell In Rng.Cells
             Me.ListBox1.AddItem Cell.Offset(0, 2).Value
             Me.ListBox2.AddItem Cell.Row
         Next Cell
    End Sub

  3. #3
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    works great thank you

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

    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
  •