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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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