Take ListBox Data and put it on spreadsheet
Results 1 to 6 of 6

Thread: Take ListBox Data and put it on spreadsheet
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Sep 2014
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Take ListBox Data and put it on spreadsheet

    I have a UserFrom with a ListBox that allows users to custom rearrange the order of items in the list box. I would then like for the items in the ListBox to be dumped into a specified location on a spreadsheet when the user presses a button in the UserForm. The line items in the list box are populated whenever they open it and the number of items in the list box is variable, so the output list size can also vary.


    Before:



    After:




    Ideal Result:


  2. #2
    MrExcel MVP
    Join Date
    Aug 2010
    Location
    Rio de Janeiro BRAZIL
    Posts
    16,223
    Post Thanks / Like
    Mentioned
    19 Post(s)
    Tagged
    8 Thread(s)

    Default Re: Take ListBox Data and put it on spreadsheet


  3. #3
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,791
    Post Thanks / Like
    Mentioned
    31 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Take ListBox Data and put it on spreadsheet

    My understanding is that you'd like to transfer the items from your ListBox to your worksheet. If this is correct, assuming that you'd like to transfer the items to Column B, starting at B2, in Sheet1, try...

    Code:
    Private Sub CommandButton1_Click()
    
        Dim LastRow As Long
        
        With Worksheets("Sheet1")
            LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
            If LastRow > 1 Then
                .Range("B2:B" & LastRow).ClearContents
            End If
        End With
        
        With Me.ListBox1
            Worksheets("Sheet1").Range("B2").Resize(.ListCount).Value = .List
        End With
        
    End Sub
    Change the references, accordingly.

    Hope this helps!

  4. #4
    MrExcel MVP
    Join Date
    Aug 2010
    Location
    Rio de Janeiro BRAZIL
    Posts
    16,223
    Post Thanks / Like
    Mentioned
    19 Post(s)
    Tagged
    8 Thread(s)

    Default Re: Take ListBox Data and put it on spreadsheet

    Besides the code provided by Domenic, if you also need a code to rearrange the items i suggest that you use a SpinButton control instead of two buttons,

    Something like this
    (adapted from Daily Dose of Excel » Blog Archive » Move Up/Move Down in a ListBox)

    Code:
    Private Sub SpinButton1_SpinDown()
         
         With Me.ListBox1
            If .ListIndex < .ListCount - 1 Then
                MoveItem 1
                .ListIndex = .ListIndex + 1
            Else
                Beep
            End If
        End With
    End Sub
    
    Private Sub SpinButton1_SpinUp()
        
        With Me.ListBox1
            If .ListIndex > 0 Then
                MoveItem -1
                .ListIndex = .ListIndex - 1
            Else
                Beep
            End If
        End With
    End Sub
    
    Private Sub MoveItem(lOffset As Long)
        Dim aTemp() As String, i As Long
        
        With Me.ListBox1
            If .ListIndex > -1 Then
                ReDim aTemp(0 To .ColumnCount - 1)
                For i = 0 To .ColumnCount - 1
                     aTemp(i) = .List(.ListIndex + lOffset, i)
                    .List(.ListIndex + lOffset, i) = .List(.ListIndex, i)
                    .List(.ListIndex, i) = aTemp(i)
                Next i
            End If
        End With
    End Sub
    Hope this helps

    M.
    Last edited by Marcelo Branco; Jan 16th, 2015 at 07:17 PM.

  5. #5
    New Member
    Join Date
    Sep 2014
    Posts
    10
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Take ListBox Data and put it on spreadsheet

    Thank you so much Domenic! That is exactly what I was wanting!

  6. #6
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,791
    Post Thanks / Like
    Mentioned
    31 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Take ListBox Data and put it on spreadsheet

    You're very welcome!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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