VBA Userform ListBox multiple selection

jordanbuchan359

New Member
Joined
Jun 15, 2018
Messages
15
Hi,

I'm attempting to build a userform, but have hit a wall..

My userform currently consists of two combo boxes and a multi-select list box. All of which pull their contents from lists within the excel book.

The combo boxes contain the available departments and disciplines, the ListBox has names. To save time, I'd like to be able to select multiple names and assign them to their relevant department / discipline. The only catch, I'd like each person to be listed in a seperate row.

For example, selecting John and Sarah, and assigning them to Stores as Admins:

Name Department Discipline
John Stores Admin
Sarah Stores Admin

Code:
Private Sub ContinueButton_Click()

    Me.Hide

    wsEmployee.Select
    
    Range("B2").End(xlDown).Offset(1, 0).Select
    
    ActiveCell.Value = SelectedEmployeeName
    ActiveCell.Offset(0, 1).Value = Combo_Department.Value
    ActiveCell.Offset(0, 2).Value = Combo_Discipline.Value
    
    Unload Me


End Sub

Private Function SelectedEmployeeName() As String

    Dim i As Integer
    
    'For loopcounter = 0 To TechName.ListCount - 1 ' need to include -1 due to zero based index
    
    For i = LBound(TechName.List) To UBound(TechName.List)
    
        If TechName.Selected(i) Then
        
        SelectedTechName = SelectedTechName & TechName.List(i)
            
        End If
    
    Next i
    
    SelectedTechName = Left(SelectedTechName, Len(SelectedTechName) - 1)

End Function

The code above does allow me to select multiple names, but places them in the same cell seperated by a comma. I'm unsure on how to proceed.

Any help would be greatly appreciated.

Thanks,
Jordan
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi & welcome to MrExcel
How about
Code:
Private Sub ContinueButton_Click()
   Dim i As Long, j As Long
   Me.Hide

   wsEmployee.Select
   
   With wsEmployee.Range("B2").End(xlDown).Offset(1, 0)
    
      For i = LBound(TechName.List) To UBound(TechName.List)
         If TechName.Selected(i) Then
            .Offset(j).Value = TechName.List(i)
            .Offset(j, 1).Value = Combo_Department.Value
            .Offset(j, 2).Value = Combo_Discipline.Value
            j = j + 1
         End If
      Next i
   End With
   Unload Me
End Sub
 
Upvote 0
Hi & welcome to MrExcel
How about
Code:
Private Sub ContinueButton_Click()
   Dim i As Long, j As Long
   Me.Hide

   wsEmployee.Select
   
   With wsEmployee.Range("B2").End(xlDown).Offset(1, 0)
    
      For i = LBound(TechName.List) To UBound(TechName.List)
         If TechName.Selected(i) Then
            .Offset(j).Value = TechName.List(i)
            .Offset(j, 1).Value = Combo_Department.Value
            .Offset(j, 2).Value = Combo_Discipline.Value
            j = j + 1
         End If
      Next i
   End With
   Unload Me
End Sub


Thanks Fluff, works a treat!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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