Naming and looping userform control items

aalansari

New Member
Joined
Oct 9, 2013
Messages
17
I'm trying to make my VBA code less cryptic and easier to debug by renaming my control items with unique names that make sense. For example, instead of "Textbox1" use "FirstName". Unfortunately, I lose the convenience of using loops to automate certain processes (like the code below).

VBA Code:
'Clear all entries
For i = 1 To 83
    Controls("Textbox" & i).Value = ""
Next i

Is there a solution where I could use unique names for my controls and still be able to loop through them?

I thought I could create a table with all the names of my control items and just loop through that list...but I wanted to see if there was a better option first.

Thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Are you trying to set all the textboxes to "", or just some of them?
 
Upvote 0
Are you trying to set all the textboxes to "", or just some of them?
Both. I posted the code snippet just as an example of how I would loop through them. In some cases, I want to copy values from a range of textboxes to a sheet while in other cases I populate textboxes with values from the sheet.
 
Upvote 0
If you preface the controls with (for instance) txtb like txtbFirstName or txtbLastName then you could use
VBA Code:
Dim Ctrl As Control
For Each Ctrl In Me.Controls
   If LCase(Ctrl.Name) Like "txtb*" Then
      Ctrl.Value = ""
   End If
Next Ctrl
 
Upvote 0
Solution
I thought I could create a table with all the names of my control items and just loop through that list...but I wanted to see if there was a better option first.

I refer to controls on a complex userform using a table that also includes additional checks for user entry requirements with error message prompts, data type & things like tab order, & which column data is posted to. This is read in to an an array which you can loop through

Dave

example

Control NameRequiredData TypeCustom MessageParentTab OrderData Column
txtForenameYesTextPlease enter the ForenameFrame102
txtSurnameYesTextPlease enter the SurnameFrame113
txtDOBYesDatePlease Enter date of DOBFrame124
txtJobTitleYesTextPlease enter the Job TitleFrame135
txtManagerYesTextPlease enter the site manager nameFrame146
txtSiteCodeYesTextPlease enter the site codeFrame157
txtPhoneNumberYesNumericPlease enter contact phone numberFrame168
 
Upvote 0
I refer to controls on a complex userform using a table that also includes additional checks for user entry requirements with error message prompts, data type & things like tab order, & which column data is posted to. This is read in to an an array which you can loop through

Dave

example

Control NameRequiredData TypeCustom MessageParentTab OrderData Column
txtForenameYesTextPlease enter the ForenameFrame102
txtSurnameYesTextPlease enter the SurnameFrame113
txtDOBYesDatePlease Enter date of DOBFrame124
txtJobTitleYesTextPlease enter the Job TitleFrame135
txtManagerYesTextPlease enter the site manager nameFrame146
txtSiteCodeYesTextPlease enter the site codeFrame157
txtPhoneNumberYesNumericPlease enter contact phone numberFrame168
This is what I had in mind, but I didn't think I could do all of that using this approach!

So, if you wanted to make sure that the user entered a Numeric value in a textbox, do you have it loop through all textboxes after update?
 
Upvote 0
If you preface the controls with (for instance) txtb like txtbFirstName or txtbLastName then you could use
VBA Code:
Dim Ctrl As Control
For Each Ctrl In Me.Controls
   If LCase(Ctrl.Name) Like "txtb*" Then
      Ctrl.Value = ""
   End If
Next Ctrl
Thanks! This works.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
This is what I had in mind, but I didn't think I could do all of that using this approach!

So, if you wanted to make sure that the user entered a Numeric value in a textbox, do you have it loop through all textboxes after update?

I do all the checks at time user presses the Enter or Submit button - You only need to loop once doing all the data checks & at same time, populate an array to output to range if all complete.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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