Loop through labels in a frame

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
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!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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