Search code examples
excelvba

VBA code that allows multiple inputs in Userform


This may sound confusing, but I hope you'll bear with me. I have a user form for data entry which has a textbox designated for employee ids and various combo boxes which is designated for their demographics. Is it possible to create a vba code that allows me to input multiple employee ids in the textbox, and whatever information that will be answered to each combo boxes will be applied to all of the employee ids?

enter image description here enter image description here

Private Sub CommandButton1_Click()
    Dim sh As Worksheet, msg As String
    
    'check for any empty required fields
    If Len(TextBox1.Value) = 0 Then msg = msg & vbLf & " - Employee ID"

    If Len(msg) > 0 Then 'anything missing?
        MsgBox "The following fields are required:" & msg, _
                vbOKOnly + vbCritical, "Missing Information"
    Else
        'OK to write to sheet
        Set sh = ThisWorkbook.Sheets("Employee Information")
        With sh.Cells(Rows.Count, "A").End(xlUp).Offset(1)
            .Resize(1, 12).Value = Array(TextBox1.Value, ComboBox1.Value, _
                                  ComboBox2.Value, ComboBox3.Value, ComboBox4.Value, _
                                  ComboBox5.Value, ComboBox6.Value, ComboBox7.Value, _
                                  ComboBox8.Value, ComboBox9.Value, ComboBox10.Value, _
                                  ComboBox11.Value)

     MsgBox "Employee Information Added", vbOKOnly + vbInformation, "ERROR"
     
    Unload Me
        End With
    End If
End Sub

Solution

  • If you enter multiple id's separated by comma you could do something like this:

    Private Sub CommandButton1_Click()
        Dim sh As Worksheet, msg As String, arr, c As Range, id
        
        'check for any empty required fields
        If Len(TextBox1.Value) = 0 Then msg = msg & vbLf & " - Employee ID"
    
        If Len(msg) > 0 Then 'anything missing?
            MsgBox "The following fields are required:" & msg, _
                    vbOKOnly + vbCritical, "Missing Information"
        Else
            'OK to write to sheet
            Set sh = ThisWorkbook.Sheets("Employee Information")
            Set c = sh.Cells(Rows.Count, "A").End(xlUp).Offset(1) '##start adding here
            arr = Split(TextBox1.Value, ",") '##split on comma to get an array
            For Each id In arr               '##loop over the array
                c.Resize(1, 12).Value = Array(Trim(id), ComboBox1.Value, _
                                      ComboBox2.Value, ComboBox3.Value, ComboBox4.Value, _
                                      ComboBox5.Value, ComboBox6.Value, ComboBox7.Value, _
                                      ComboBox8.Value, ComboBox9.Value, ComboBox10.Value, _
                                      ComboBox11.Value)
                Set c = c.Offset(1)          '##next output row
            Next id
    
            MsgBox "Employee Information Added", vbOKOnly + vbInformation, "ERROR"
            Unload Me
        End If
    End Sub