I have 4 textboxes on a userform. The value of all 4 are transferred into one cell separated by a break(Char(10)) and a prefix "Error1:" string, but if only the first textbox has a value and the rest are empty, the prefix for the other textbox appears on the cell. I'd like to know if the empty textboxes can be skipped, and not transfer the prefix of those to the cell.
I'm using an If statement and "&" to add all textboxes into one cell.
a = Error1Box.Value
b = Error2Box.Value
c = Error3Box.Value
d = Error4Box.Value
If a = "" Then
'Do Nothing
Else
Cells(emptyRow, 16).Value = "Error1: " & a & Chr(10) & "Error2: " & b & Chr(10) & "Errro3: " & c & Chr(10) & "Error4: " & d
End If
I expect: If only Error1Box has value then the cell will be Error1: description...
but what I'm getting is:
Error1: Description...
Error2:
Error3:
Error4:
so if the rest of the textboxes are empty them nothing else shows except the ones with values.
you could iterate through your textboxes and build the error string when appropriate:
Dim errString As String
Dim iTb As Long
For iTb = 1 To 4
With Me.Controls("Error" & iTb & "Box")
If .Value <> vbNullString Then errString = errString & Chr(10) & "Error" & iTb & ": " & .Value
End With
Next
If errString <> vbNullString Then Cells(emptyRow, 16).Value = Mid$(errString, 2)