简体   繁体   中英

Excel VBA - for each label / user form

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? (Ie run as a small window without the menu or anything other than the area of what would be the "userform".)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM