combining multiple userform textbox values into one cell

Awill

I have a userform that is filled out and enters the data on a spreadsheet. One feature I've added is frame of 10 textboxes which remain hidden unless data is entered in the previous box, allowing up to 10 entries. Because the user is entering participants' names in these text boxes, I want them all to fill the same cell of the spreadsheet, separated by commas. At first I entered the code without thinking and would receive 9 commas in the cell even when no names were added. I've since managed to get the second text box value preceded by a comma added to the cell but cannot get the value of any additional textboxes (3-10) to do the same.

ws.Range("I" & LastRow).Value = tbPart1.Value
If Not IsEmpty(tbPart2.Value) Then
ws.Range("I" & LastRow).Value = tbPart1.Value & "," & tbPart2.Value
ElseIf Not IsEmpty(tbPart3.Value) Then
ws.Range("I" & LastRow).Value = tbPart1.Value & "," & tbPart2.Value & "," & tbPart3.Value

End If
Chrowno

Looping through all Textboxes and checking for its value:

Dim i As Integer

For i = 1 To 10

    'Loop through all 10 Textboxes and add its value to the cell
    If Not Controls("tbPart" & i).Value = "" Then

        'Check if cell is empty
        If ws.Range("I" & LastRow).Value = "" Then
            ws.Range("I" & LastRow).Value = Controls("tbPart" & i).Value
        Else
            ws.Range("I" & LastRow).Value = _
            ws.Range("I" & LastRow).Value & ", " & Controls("tbPart" & i).Value
        End if
    End If
Next i

Code is not tested.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related