Loop through Textboxes

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
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?
 
So, here’s my approach: I use an array with the Textbox controls. I put the array as Public in the Userform’s code This has a few advantages in maintenance:


  1. There is no requirement on the name or number of textboxes. What would you do if the boss says “We need to add a TextBox named WhatTB because the new program we purchased last week requires it”
This would require sticking the new TextBox on the form and in the array. Then, of course, testing


  1. What would you need to do if you needed to make this a function that returned the CC line sorted?
  2. How about removing duplicates in the CC?
  3. What needs to be done to require at lease one filled in TextBox?

Lots more, I'm sure.

I feel like giving stars for code that can be easily maintained.


This all goes into the UserForm module:

Code:
Public tbarray
 
Private Sub UserForm_Initialize()
    tbarray = Array(Me.TextBox1, Me.TextBox2, Me.TextBox3, Me.TextBox4, Me.TextBox5)
End Subs
 
Private Sub CommandButton1_Click()
    Dim dictCC As Object: Set dictCC = CreateObject("Scripting.Dictionary")
    Dim dictEmpt As Object: Set dictEmpt = CreateObject("Scripting.Dictionary")
    Dim tb
    For tb = 0 To UBound(tbarray)
        With UserForm1.Controls(tbarray(tb).Name)
            Select Case Len(Trim(.Text)) = 0
                Case True
                    dictEmpt.Add tb, tbarray(tb).Name
                Case Else
                    dictCC.Add tb, tbarray(tb).Text
            End Select
        End With
    Next
    MsgBox "CC line:" & vbCrLf & Join(dictCC.items, ", ")
    MsgBox "Empty TexBoxes:" & vbCrLf & Join(dictEmpt.items, vbCrLf)
End Sub
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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