PDA

View Full Version : Solved: Loop Through a UserForms Controls and Change TextBox Properties



Saladsamurai
09-10-2009, 08:49 AM
Lets say I have a UserForm and I want to loop throuh it controls and change only the widths of the TextBoxes. It must be something silly like

For Each TextBox In Me.Controls

...Something....


Next TextBox

Bob Phillips
09-10-2009, 09:36 AM
For Each ctl In Me.Controls

If TypeName(ctl) = "TextBox" Then

...
End If
Next ctl

Saladsamurai
09-10-2009, 09:41 AM
Hi xld. The problem is that I want to change the Width Property. Sorry I wasn't too clear on that.

I just don't know what the "..." part is to change the width.

Bob Phillips
09-10-2009, 09:49 AM
Guess what, it is Width!

Saladsamurai
09-10-2009, 10:08 AM
I am not sure where to put this:

For Each ctl In Me.Controls

If TypeName(ctl) = "TextBox" Then

ctl.Width = 24
End If
Next ctl

If I put it in the "OKButton_Click" Sub, it only changes the textbox widths AFTER I click 'OK'.

If I place it in it's own module, I get an Error: 'Improper Use fo Me.Controls'

Any ideas?

Bob Phillips
09-10-2009, 10:22 AM
When do YOU want it to happen? That will determine where it goes, but it has to be in the form because you are using Me.

Saladsamurai
09-10-2009, 11:08 AM
Perhaps I should restate the problem.

Problem: I have a UserForm named "UserForm1" and on it I have 48 TextBoxes named "TextBox1. . . TextBox48"

After testing the userform, I have realized that the default widths for said Textboxes are too narrow.

I would like to change all of the widths of all of TextBoxes to '24.'

I could go through manually, select each Textbox with my cursor, go to the Properties Window and change the 'Width' property of each of them to 48. But there should be a more efficient way.

It is that more efficient way that I am seeking here now. It should be a one-time thing. I would like to run the code once and then save the defaults so that everytime I open that UserForm, the TextBoxes are of the proper width = 24.

Bob Phillips
09-10-2009, 02:02 PM
You can select all 48 in the designer and set their width in one go. As you select multiple items, only the shared properties will show, width will be one.