Loop through labels in a frame

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to work out how to loop through all labels contained in a frame on a userform.

I currently have this:

VBA Code:
For i = 3 To 10
Me.Controls("Frameobject" & i).Enabled = False
Me.Controls("textrationale" & i).Enabled = False
Me.Controls("textrationale" & i).Value = ""
Next
Exit Sub
End If

What I need to add, and what I am struggling to work out, is how to loop through all labels within the frame and disable them. There are a lot of labels in each frame so want to avoid having to list each one and disable it, which I know I can do as a last resort.

Thank you!
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

davesexcel

Well-known Member
Joined
Feb 26, 2006
Messages
1,089
Loop through the controls in the frame
VBA Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As Control
    For Each Ctrl In UserForm1.Frame1.Controls
        If TypeOf Ctrl Is MSForms.Label Then
            With Ctrl
                .Caption = "yep"
                .Enabled = False
            End With
        End If
    Next Ctrl
End Sub
 

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
Dave, thanks - how can I apply it to the specific frame that is being referenced in the loop though, rather than having to specify by name which Frame?
 

davesexcel

Well-known Member
Joined
Feb 26, 2006
Messages
1,089
Loop through frames and loop through controls in frame

VBA Code:
Private Sub CommandButton1_Click()
    Dim fm As Integer
    Dim ctrl As Integer
    For fm = 0 To Me.Controls.Count - 1
        If TypeOf Me.Controls(fm) Is msforms.Frame Then
            For ctrl = 0 To Me.Controls(fm).Controls.Count - 1
                If TypeOf Me.Controls(fm).Controls(ctrl) Is msforms.Label Then
                    With Me.Controls(fm).Controls(ctrl)
                        .Caption = "Disabled"
                        .Enabled = False
                    End With
                End If
            Next ctrl
        End If
    Next fm
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,126,952
Messages
5,621,798
Members
415,856
Latest member
jimb2k

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
Top