+ Reply to Thread
Results 1 to 5 of 5

Making A Listbox Select A Cell.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2004
    Location
    Chicago
    Posts
    31

    Question Making A Listbox Select A Cell.

    hi,

    i'm working on a userform. it has a listbox. when the form starts up, it looks in column A for any cells that have something in it (starting after row 2).

    for each cell that has something in it, a line is added to the listbox. the line added in the listbox is whatever the value of the cell and the cell one column right of it was.

    here's the code:

    Private Sub UserForm_Initialize()
    
    With Range("A:A")
        For Each r In Range("A:A")
        If Not r.Value = 0 And r.Row > 2 Then
        ListBox1.AddItem r.Value & " " & r.Offset(0, 1).Value, -1
        End If
        Next
    End With
        
        ListBox1.ListIndex = ListBox1.ListCount - 1
        
    End Sub
    now, i'm trying work on code that will make it so that when i click on a line in the listbox, the corresponding cell in row A will be selected. but i don't know how to do this. any ideas?

    thankyou

  2. #2
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    Add this click event code to your userform

    Private Sub ListBox1_Click()
        For x = 0 To ListBox1.ListCount
            
            If ListBox1.Selected(x) = True Then 
                Cells(x + 3, 1).Select
            End If
        
        Next x
    End Sub
    Right now it only works correctly on contiguous data in A but I'm working on getting it to recognize skipped cells to deal with non contiguous data.

    Hope this helps

  3. #3
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    OK, I figured out a better way which ignores blank entries in column A
    use this in the listbox click event rather than the code I posted earlier

    Private Sub ListBox1_Click()
        For x = 0 To Cells(Rows.Count, "A").End(xlUp)
          If ListBox1.Value = Cells(x + 3, 1) & " " & Cells(x + 3, 2) Then
            Cells(x + 3, 1).Select
          End If
        Next x
     End Sub
    Hope this helps

  4. #4
    Registered User
    Join Date
    03-19-2010
    Location
    mexico
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Making A Listbox Select A Cell.

    hey i'm looking for the same... but i want to select multiple cells with the option multiselect...

    can you help me???

  5. #5
    Forum Contributor marc5354's Avatar
    Join Date
    11-10-2009
    Location
    India
    MS-Off Ver
    Excel 2003 & Excel 2007
    Posts
    217

    Re: Making A Listbox Select A Cell.

    mister190

    Read forum rules before you post a comment, Start a new post for your query.

    Marc

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1