Userform Controls VBA

bewsh1987

Board Regular
Joined
Sep 3, 2013
Messages
232
Hi All,

Need your help I have 48 Labels on a userform with the name Day1,Day2,Day 3 Ect. Ect. Day47 and Day48

I want to avoid having 48 Sub Procedures example below

Code:
Private Sub Day1_Click()
Userform1.Hide
'>>>Call Function and Call Sub Will Go Here<<<
End Sub

Is there a way to have a userform complete a procedure action based on what label was clicked maybe an event handler?

Thank you

Ian
 
I misunderstood what you wanted. If you want one Click event procedure that applies to all your labels you can use a Class Module. Try:

Code:
'Class Module named ClassLabels

Public WithEvents LabelGroup As MSForms.Label

Private Sub LabelGroup_Click()
'   Replace the next line with your code
    MsgBox "You clicked " & LabelGroup.Name
End Sub

'UserForm Module

Dim Labels() As New ClassLabels

Private Sub UserForm_Initialize()
    Dim LabelCount As Integer
    Dim Ctrl As Control
    LabelCount = 0
    For Each Ctrl In UserForm1.Controls
        If TypeName(Ctrl) = "Label" Then
            LabelCount = LabelCount + 1
            ReDim Preserve Labels(1 To LabelCount)
            Set Labels(LabelCount).LabelGroup = Ctrl
        End If
    Next Ctrl
End Sub
 
Last edited:
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
1) I assume that the function is in your Userform CLass Module. Declare it as a Friend routine.

2) Create a class module called clsLabel_Event, and put in the following code
Code:
Option Explicit

Private WithEvents ob As MSForms.Label
Private CallBackParent As [userform_name]


Private Sub ob_Click()

    callbackparent.hide
    Call CallBackParent.partNames()

End Sub

Friend Sub WatchControl(oControl As MSForms.Label)

    Set ob = oControl
    Set CallBackParent = ob.parent

End Sub

3)add the following code to your userform class module (I'm free forming this w/o testing, so be sure to debug before running):
Code:
option explicit

Private collLabs As Collection

Private Sub UserForm_Initialize()

    dim ctrl as msforms.control
    Dim OBE As clsLabel_Event

    Set collLabs = New Collection

    for each ctrl in me.controls

        if typename(ctrl) = "Label" then

           Set OBE = New clsLabel_Event
        
           collLabs.Add OBE
        
            Call OBE.WatchControl(ctrl)

        end if

    next ctrl

end sub



My function was in the userfrom modules I created a userfrom class module and it worked like a charm

Thanks You so much this has really helped me move forward
 
Last edited:
Upvote 0
I misunderstood what you wanted. If you want one Click event procedure that applies to all your labels you can use a Class Module. Try:

Code:
'Class Module named ClassLabels

Public WithEvents LabelGroup As MSForms.Label

Private Sub LabelGroup_Click()
'   Replace the next line with your code
    MsgBox "You clicked " & LabelGroup.Name
End Sub

'UserForm Module

Dim Labels() As New ClassLabels

Private Sub UserForm_Initialize()
    Dim LabelCount As Integer
    Dim Ctrl As Control
    LabelCount = 0
    For Each Ctrl In UserForm1.Controls
        If TypeName(Ctrl) = "Label" Then
            LabelCount = LabelCount + 1
            ReDim Preserve Labels(1 To LabelCount)
            Set Labels(LabelCount).LabelGroup = Ctrl
        End If
    Next Ctrl
End Sub

Thank you for this I am still learning and to be honest I've not used class modules in the past and didn't know what potential class modules had, Great code

Thank you
 
Upvote 0

Forum statistics

Threads
1,216,111
Messages
6,128,899
Members
449,477
Latest member
panjongshing

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