How do you use a multiselect listbox1 to place data in a specific cell using VBA

knharris75

New Member
Joined
Jun 21, 2010
Messages
2
I have a userform that contains a listbox that needs to do the following:
1. Allow multiple data elements to be selected
2. Copy those selected items to the active cell
3. Place all the data elements in the active cell.

Here is the code I have written, I just need to figure out how to make it work for multiselection, HELP ME PLEASE!!!!!!!!!!!

Private Sub CommandButton2_Click()
Sheet2.Activate
ActiveCell.Offset(0, 0).Range("A1").Select
ActiveCell.Value = UserForm1.ListBox1.Value
UserForm1.ListBox1.Value = vbNullString
UserForm_Click
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Private Sub CommandButton2_Click()
Dim i As Long
Dim data As String

	With UserForm1.ListBox1
        For i = 0 To .ListCount - 1 
            If .Selected(i) Then 
                data = data & .List(i) & " " 
            End If 
        Next i 
    End With 

    Sheet2.Range("A1").End(xlDown).Offset(1, 0).Value2 = data
End Sub
 
Upvote 0
XLD. thanks for the swift response. i have entered the code you suppplied and I recieve a run time error "1004" Application-defined or object defined error. What am I doing wrong?
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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