Dump multiple listbox values into one cell

rvinci

Board Regular
Joined
Jan 28, 2013
Messages
52
I've searched and haven't been able to find an answer. I have a lengthy userform that I am adding a listbox into which will allow for multiple selections. On submit, I need all listbox selections to be populated into one cell. The basic coding I have below isn't working. Any help is appreciated.

Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("MASTER - CLIENT ROSTER")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

'copy the data to the database
With ws

.Cells(iRow, 1).Value = Me.TextBox1.Value
.Cells(iRow, 2).Value = Me.ComboBox1.Value
.Cells(iRow, 3).Value = Me.ComboBox2.Value
.Cells(iRow, 4).Value = Me.ComboBox3.Value
.Cells(iRow, 5).Value = Me.ComboBox4.Value
.Cells(iRow, 6).Value = Me.ComboBox5.Value
.Cells(iRow, 7).Value = Me.TextBox2.Value
.Cells(iRow, 8).Value = Me.ComboBox6.Value
.Cells(iRow, 10).Value = Me.ListBox1.Value

End With

'clear form
Unload Me

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Is your listbox just a single column?
 
Upvote 0
Is your listbox just a single column?
Yes, the listbox is a single is a single column with 10 separate values. However many values are selected would all need to populate into one cell for each userform submission....if that makes sense.
 
Upvote 0
Ok, how about
VBA Code:
Private Sub CommandButton1_Click()
   Dim iRow As Long
   Dim ws As Worksheet
   Dim i As Long
   Dim Txt As String
   
   With Me.ListBox1
      For i = 0 To .ListCount - 1
         If .Selected(i) = True Then Txt = Txt & ", " & .List(i)
      Next i
   End With
   If Txt <> "" Then Txt = Mid(Txt, 2)
   

   Set ws = Worksheets("MASTER - CLIENT ROSTER")
   
   'find first empty row in database
   iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
   
   'copy the data to the database
   With ws
   
      .Cells(iRow, 1).Value = Me.TextBox1.Value
      .Cells(iRow, 2).Value = Me.ComboBox1.Value
      .Cells(iRow, 3).Value = Me.ComboBox2.Value
      .Cells(iRow, 4).Value = Me.ComboBox3.Value
      .Cells(iRow, 5).Value = Me.ComboBox4.Value
      .Cells(iRow, 6).Value = Me.ComboBox5.Value
      .Cells(iRow, 7).Value = Me.TextBox2.Value
      .Cells(iRow, 8).Value = Me.ComboBox6.Value
      .Cells(iRow, 10).Value = Txt
   
   End With

'clear form
Unload Me
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Private Sub CommandButton1_Click()
   Dim iRow As Long
   Dim ws As Worksheet
   Dim i As Long
   Dim Txt As String
  
   With Me.ListBox1
      For i = 0 To .ListCount - 1
         If .Selected(i) = True Then Txt = Txt & ", " & .List(i)
      Next i
   End With
   If Txt <> "" Then Txt = Mid(Txt, 2)
  

   Set ws = Worksheets("MASTER - CLIENT ROSTER")
  
   'find first empty row in database
   iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
  
   'copy the data to the database
   With ws
  
      .Cells(iRow, 1).Value = Me.TextBox1.Value
      .Cells(iRow, 2).Value = Me.ComboBox1.Value
      .Cells(iRow, 3).Value = Me.ComboBox2.Value
      .Cells(iRow, 4).Value = Me.ComboBox3.Value
      .Cells(iRow, 5).Value = Me.ComboBox4.Value
      .Cells(iRow, 6).Value = Me.ComboBox5.Value
      .Cells(iRow, 7).Value = Me.TextBox2.Value
      .Cells(iRow, 8).Value = Me.ComboBox6.Value
      .Cells(iRow, 10).Value = Txt
  
   End With

'clear form
Unload Me
End Sub
That did it, thank you so much!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,787
Messages
6,126,900
Members
449,348
Latest member
Rdeane

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