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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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