Need code to list out all selected items from a listbox

atf32

Board Regular
Joined
Apr 13, 2011
Messages
157
Looking for code that will list out all the selected items from a listbox and place them in a range on the surface of excel.:confused: So if the listbox lists 10 different fruits and the user selects 3 of them, then only the 3 selected fruits will be listed in cells "A1", "A2" and "A3". Then everytime the user selects or deselects and item, the displayed list is updated.:rolleyes:
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
add this in your worksheet module, please note the name in bold of the listbox which probably you will need to change


Code:
Const ExportCell As String = "A1" ' <-- where your selection is printed


Private Sub myListBox_Change()
Dim Ndx As Long, ASelection As Variant
With Me
    .Columns("A").ClearContents
    With .[SIZE=3][FONT=arial black][B]myListBox[/B][/FONT][/SIZE]
        ReDim ASelection(0 To .ListCount - 1)
        For Ndx = 0 To .ListCount - 1
            If .Selected(Ndx) Then
                ASelection(Ndx) = .List(Ndx)
            Else
                ASelection(Ndx) = Chr$(11)
            End If
        Next
    End With
    ASelection = Filter(ASelection, Chr$(11), False, vbBinaryCompare)
    If Not UBound(ASelection) = -1 Then .Range(ExportCell).Resize(UBound(ASelection) + 1).Value = Application.Transpose(ASelection)
End With
End Sub
On the spreadsheet.
 
Upvote 0
Awesome that worked exactly how I wanted it to. :)
Also, thank you for the quick response.
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top