+ Reply to Thread
Results 1 to 7 of 7

Listbox selection in form to table

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2014
    Location
    Denmark
    MS-Off Ver
    365 (Win and Mac)
    Posts
    20

    Listbox selection in form to table

    Hi All.

    Have this problem with a listbox. When I select a item in my listBox (Located in a form) and then execute command to paste it into a table. It is pasting the last item on the list everytime. What am I doing wrong? Do I need to index the list, if so, then how? or is there another fix for this.
    This is the code I am using, the ListBox have two columns.

    Private Sub cmdBook_Click()
    Dim lrDT As Long
    
    lrDT = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
    
        Dim i As Long
        For i = 0 To Me.ListBox1.ListCount - 1
            Sheets("Sheet3").Cells(lrDT + 1, "A") = ListBox1.Column(0, i)
            Sheets("Sheet3").Cells(lrDT + 1, "B") = ListBox1.Column(1, i)
        Next i
    
    End Sub

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Listbox selection in form to table

    Maybe:

    Private Sub cmdBook_Click()
    Dim lrDT As Long
    
    lrDT = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
    
        Dim i As Long
        For i = 0 To Me.ListBox1.ListCount - 1
            Sheets("Sheet3").Cells(lrDT + 1 + i, "A") = ListBox1.Column(0, i)
            Sheets("Sheet3").Cells(lrDT + 1 + i, "B") = ListBox1.Column(1, i)
        Next i
    
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    06-25-2014
    Location
    Denmark
    MS-Off Ver
    365 (Win and Mac)
    Posts
    20

    Re: Listbox selection in form to table

    Just tried. Dosenīt work sorry.
    It adds the hole record from the listbox to the table. Funny..
    But thanks for trying..

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Listbox selection in form to table

    Do you only want to add the selected item/row in the listbox to the table?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    06-25-2014
    Location
    Denmark
    MS-Off Ver
    365 (Win and Mac)
    Posts
    20

    Re: Listbox selection in form to table

    Hey Norie..

    Yes I only want to add the selected item from the list to the table.
    Actually there is two ListBoxes in the form and two TextBoxes, but the Textboxes add data fine. It is just the select thing in listbox that drives me crazy..


    Sent from my iPad using Tapatalk

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Listbox selection in form to table

    Don't use a loop and use the ListIndex property of the listbox to identify the selected item from the list.

    Something like this, untested, code.
    Private Sub cmdBook_Click()
    Dim lrDT As Long
    Dim idx As Long
    
        idx = Me.ListBox1.ListIndex
         
        If idx = -1 Then Exit Sub ' check something has been selected
    
        lrDT = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row + 1 
        
        
        
        Sheets("Sheet3").Cells(lrDT, "A") = ListBox1.List(idx,0)
        Sheets("Sheet3").Cells(lrDT, "B") = ListBox1.List(idx,1)
    
    End Sub

  7. #7
    Registered User
    Join Date
    06-25-2014
    Location
    Denmark
    MS-Off Ver
    365 (Win and Mac)
    Posts
    20

    Re: Listbox selection in form to table

    Hey Norie.

    It works. Thanks you so much..!! And thank you for your time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. User Form Controls - Connect ComboBox Selection to Listbox
    By wstring in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-15-2013, 04:15 PM
  2. How do I get value of a multi-selection form listbox?
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-15-2010, 03:57 AM
  3. Populate User Form from ListBox Selection
    By Leah in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-21-2009, 03:08 AM
  4. [SOLVED] Listbox selection creates form
    By Jennifer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-09-2006, 03:55 AM
  5. [SOLVED] Which User Form Listbox selection just checked?
    By Don Wiss in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2005, 09:05 PM

Tags for this Thread

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