Loop through Textboxes

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,421
Office Version
  1. 2016
Platform
  1. Windows
I have 5 textboxes on a userform and I want to evaluate if they have text entered.

The values are used as a 'CC' list for an email message, so I also need a semi-colon added after each textbox.

Can someone show me how this could work?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Like this:
Rich (BB code):
    Dim cc  As String
    Dim ctl As Object
    
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.TextBox Then
            If Len(ctl.Value) > 0 Then cc = cc & ctl.Value & ";"
        End If
    Next ctl
    
    MsgBox cc
 
Upvote 0
Are the 5 textboxes the only textboxes on the userform?
 
Upvote 0
Thanks for the replies - there are more than 5 Textboxes, but the ones that this applies to are named TextCC1, 2, etc...
 
Upvote 0
Try this.
Code:
Dim strCCList As String
Dim I As Long

    For I = 1 To 5
        With Me.Controls("TextCC" & I)
            If .Value <> "" Then
                strCCList = strCCList & .Value & ";"
            End If
        End With
    Next I

    If strCCList <> "" Then
        MsgBox strCCList
    End If
 
Upvote 0
Just so you have alternatives, here is Kyle's approach modified to find just your 5 TextBoxes using the structured names you gave them...
Code:
Dim cc  As String
Dim ctl As Object
    
For Each ctl In Me.Controls
    If TypeOf ctl Is MSForms.TextBox Then
        If ctl.Name Like "TextCC*" Then cc = cc & ctl.Value & ";"
    End If
cNext ctl
    
MsgBox c
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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