Loop through controls on a userform

shaneherron

Board Regular
Joined
Nov 3, 2009
Messages
51
Hey guys, I hope someone can help me with this one cuz I'm stumped.

I have a userform that contains several textboxes, checkboxes, and comboboxes. There are also some command buttons, one of which says clear all. What I want to do is loop through the controls and clear the contents or change the value to false, depending on the type. I can do this by type the name.value = "", but there are alot of controls. If possible, a loop would be much more effecient.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi Shane,

Just so you know, your approach of using an If...ElseIf...End If block is absolutely fine. The problem is the execution:
Rich (BB code):
    Dim ctrl As MSForms.Control
 
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is MSForms.CheckBox Then
            ctrl.Value = False
        ElseIf TypeOf crtl Is MSForms.TextBox Then
            ctrl.Value = ""
        ElseIf TypeOf ctro Is MSForms.ComboBox Then
            ctrl.Value = ""
        End If
 
    Next ctrl
You can see the typos highlighted in red.

To avoid this sort of thing you should add an Option Explicit statement to the top of your code modules. With an Option Explicit statement in place the code will not compile with these typos and they will be quickly highlighted to you so you can quickly identify and correct them.

Going forward you can have the Option Explicit statement automatically inserted for you in new code modules if, in the VBE, you go to Tools | Options | Editor tab and tick the "Require Variable Declaration" checkbox. There are other benefits to this too, such as your code in general will run more efficiently. I strongly recommend this.

The only other consideration you should give is the one we already mentioned: your code will also clear any optionbuttons (or togglebuttons) because TypeOf ctrl Is MSForms.CheckBox "can't tell the difference" between a Checkbox, an OptionButton and a ToggleButton.

Hope that helps...
 
Upvote 0
Wow, Thanks Norie, that did the job.

I have never seen a select case done like that. I'll remember that.
I actually wouldn't recommend using Select Case like that on a regular basis.

I know it works but it's not 'standard' if you like, and can be rather confusing.:)
 
Upvote 0
Normally TypeOf() is preferrable, but I think here you have to use VBA.TypeName() to distinguish for a MSForms.CheckBox.

Of course, as has just been pointed out to me, one could use
Code:
            If TypeOf ctl Is MSForms.CheckBox Then
                If Not TypeOf ctl Is MSForms.ToggleButton Then
                    If Not TypeOf ctl Is MSForms.OptionButton Then
                        'we know we have a checkbox
 
Upvote 0
Try this.
Code:
Dim ctrl As MSForms.Control
 
For Each ctrl In Me.Controls
    Select Case True
        Case TypeOf ctrl Is MSForms.CheckBox
            ctrl.Value = False
        Case TypeOf ctrl Is MSForms.TextBox
            ctrl.Value = ""
        Case TypeOf ctrl Is MSForms.ComboBox
            ctrl.Value = ""
    End Select
    
Next ctrl
with many thanks of Norie , also you can use the below code that belong to excellency PGC (above Post) with little bit change as below:
Code:
Private Sub CommandButton1_Click()
Dim ctrl As MSForms.Control
 
For Each ctrl In UserForm1.Controls
   
    On Error Resume Next
    If ctrl.Value <> "" Then
 
 ctrl.Value = ""
    End If
Next ctrl

End Sub

with thank S.M.Mir (Iran-Tehran)
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,938
Members
452,949
Latest member
beartooth91

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