1

I have an Excel Userform and on it I have several Labels, some of which are on a Frame.

Is the following possible?

For each label [that is on the frame]
    [code]
Next label

If yes, how do you do that? Or can you group some of them and then control them as members of that group like

For each label in [groupname]
    [code]
Next label

? If yes, how do you do that? I tried to group them, but no success.

I would like that userform to serve as a user input interface with a dashboard-kind of thing on it too... Of course that would be much easier to do on a worksheet, but then you couldn't really make it look nice and userform-like, could you? (I.e. run as a small window without the menu or anything other than the area of what would be the "userform".)

1
  • What you're trying to achieve is possible thanks to the collections Commented Jun 14, 2015 at 15:20

2 Answers 2

3

Try this for looping all labels within form:

For Each ctrl In Me.Controls
    If TypeName(ctrl) = "Label" Then
        Debug.Print ctrl.Name
    End If
Next ctrl

And this for looping labels within a frame (name Frame1 in my case)

For Each ctrl In Me.Frame1.Controls
    If TypeName(ctrl) = "Label" Then
        Debug.Print ctrl.Name
    End If
Next ctrl
Sign up to request clarification or add additional context in comments.

1 Comment

@metrazol Control type documentation has good information on this as well
0

To expand it a bit. Works also for MS Access as following to map the used labels.

Form OnOpen event :

 Private Sub Form_Open(Cancel As Integer)
 List_Labels Me.Form
 End Sub

Called Subroutine (i keep them in a separate module):

Sub List_Labels(ByRef frm As Form)
Dim ctrl As Control

For Each ctrl In frm.Controls
    If TypeName(ctrl) = "Label" Then
        Debug.Print frm.Name & " - " & ctrl.Name & " value: " & ctrl.Caption
    End If
Next ctrl
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.