For each Control in Userform

matthew230

Board Regular
Joined
Jan 2, 2006
Messages
152
Hi,

Can someone help me quickly with learning a new technique.

I want to be able to quickly update a number of controls on a userform.

I have manged to work out:

For each control in userform1.controls
control.enabled = false
next

etc.

Can I define a type of control, eg for all optionbuttons, labels or textboxes etc.

Also is there a way to define the value. I want to say for each checkbox, if it has a value = true then to enable the control.

I tried the following but it doesn't work

for each control in userform1.controls
if control.value = true then
control.enabled = true
else
end if
next

Many thanks for all your help.

Matt
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi,

Here is an example of some controls - the controls are emptied here:

Code:
Private Sub CommandButton1_Click()
    Dim ctrElement As Control
    For Each ctrElement In Controls
        Select Case TypeName(ctrElement)
            Case "TextBox": ctrElement = ""
            Case "CheckBox": ctrElement = False
            Case "ListBox", "ComboBox"
            If ctrElement.RowSource = "" Then
                ctrElement.Clear
            Else
                ctrElement.ListIndex = -1
            End If
        End Select
    Next
End Sub
Case_Germany
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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