Looking for an easier way of doing things

Mhat

Active Member
Joined
May 23, 2003
Messages
448
Hi all,

If you want to perform an action on multiple items on a user form, do they have to be performed individually or is there a way to perform the operation as a group. For example, lets say that I want to set all text boxes on a user form to blank when I click a command button. I am currently doing this:

Code:
Tb_TailNo.Value = ""
        Tb_Register.Value = ""
        Tb_Owner.Value = ""
        Tb_Address.Value = ""
        Tb_City.Value = ""
        Tb_State.Value = ""
        Tb_Country.Value = ""
        Tb_ZIP.Value = ""
        Tb_MAKE.Value = ""
        Tb_Model.Value = ""
        Tb_COLOR.Value = ""
        Tb_Trim.Value = ""
        Tb_PaxSeats.Value = ""

Is there an easier way to do this?

Regards,

Mike
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Take a look at the Controls collection of the userform.

Code like the followning will go through all the controls.
Code:
Dim ctl

For Each ctl In Me.Controls ' Me is a reference to the userform
     ' do something with the control ctl eg clear it
Next ctl
There are various ways you could test if the control is one you want to clear.

Perhaps in your case something like this:
Code:
If Left(ctl.Name) = "Tb" Then ctl.Value = ""
 
Upvote 0
Or without testing for the control name, but just the control type...
Code:
Private Sub CommandButton1_Click()
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "TextBox" Then Ctrl.Value = ""
Next Ctrl
End Sub
Hope it helps,
Dan
 
Upvote 0
Thank you,

It works great and it certainly is easier. Can I assume that if I wanted to perform an operation on another control that I would replace "Textbox" with
say "listbox"?

Code:
If TypeName(Ctrl) = "TextBox" Then Ctrl.Value = ""
 
Upvote 0
Sure, TextBox, ListBox, ComboBox...
When using it for CheckBox & OptionButton, you'll want to use
Value = False instead of Value = "".

Dan
 
Upvote 0

Forum statistics

Threads
1,203,605
Messages
6,056,269
Members
444,853
Latest member
sam69

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