Format User Form Controls From within another module

Peter Ren

New Member
Joined
May 1, 2018
Messages
3
Hi, I have some code that I used to format the controls of a form. I know want to re-use that code to format other/different User Forms

My code is:
Code:
Private Sub UserForm_Initialize()
    Dim ctrl As Control
    
   'Loop Through each LABEL control on UserForm->frmTxAdd
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "Label" Then
            ctrl.BackColor = RGB(241, 248, 233)
        End If
    Next ctrl
End Sub
I have replaced the above FOR loop with:
Call FormColourGreen(Me.Name)

In Module 1, I have the following code (doesn't work)
Code:
Sub FormColourGreen(FrmName)
    Dim ctrl As Control
    
    '   Set background colour - very light green
    'Loop Through each LABEL control on UserForm
    For Each ctrl In frmName.Controls
        If TypeName(ctrl) = "Label" Then
            ctrl.BackColor = RGB(241, 248, 233)
        End If
    Next ctrl
End Sub
Your help will be very much appreciated Many Thanks
 
Last edited by a moderator:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Instead of passing the name of the userform to the other sub pass the actual userform itself.

Userform code

Code:
Private Sub UserForm_Initialize()
    Call FormColourGreen(Me)
End Sub

Module code
Code:
Sub FormColourGreen(Frm As Variant)
Dim ctrl As Control

    ' Set background colour - very light green
    ' Loop Through each LABEL control on UserForm frm
    
    For Each ctrl In Frm.Controls
        If TypeName(ctrl) = "Label" Then
            ctrl.BackColor = RGB(241, 248, 233)
        End If
    Next ctrl

End Sub
 
Upvote 0
Or you could use a sub like this to set the default values of the Labels, and not have to set it in the Initialize event.

Code:
Sub MakeUFDefaultGreen()
    Dim oneControl As Object
    
    With ThisWorkbook.VBProject.VBComponents("Userform1").Designer: Rem adjust
        For Each oneControl In .Controls
            If TypeName(oneControl) = "Label" Then
                oneControl.BackColor = RGB(241, 248, 233)
            End If
        Next oneControl
    End With
End Sub
 
Upvote 0
Instead of passing the name of the userform to the other sub pass the actual userform itself.

Thanks Norrie worked a treat, you have helped an old man who is trying o use his brain - use it or lose it apparently
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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