Introduction
In VBA UserForms, labels play a crucial role not just as static text, but as tool titles or status messages conveying processing results.
While label formatting is typically set in the Properties window, changing it dynamically with VBA code allows for a richer and more intuitive user interface. For example, you can programmatically change a label to green upon success or red upon failure.
This article explains the basic methods for setting various formats (display text, font, text color, background color, etc.) of Label controls using VBA properties.
VBA Sample Code to Set Label Formatting
This macro formats a label named TitleLabel placed on a UserForm named DisplayForm to look like a stylish header.
This code should be written within the UserForm_Initialize() event, which runs when the form is loaded, or in the macro that shows the form before FormName.Show.
UserForm Code
' Event executed when the form is initialized
Private Sub UserForm_Initialize()
' Specify the target label using a With block
With Me.TitleLabel
' .Caption: Set the text to display
.Caption = "Welcome!"
' .Font object: Set font-related formatting
.Font.Name = "Arial" ' Specify the font name
.Font.Bold = True ' Make it bold
.Font.Size = 16 ' Specify font size in points
' .ForeColor: Set the text color
.ForeColor = RGB(255, 255, 255) ' Specify white using the RGB function
' .BackColor: Set the background color
.BackColor = RGB(6, 128, 67) ' Specify dark green using the RGB function
' .TextAlign: Set the horizontal alignment of the text
.TextAlign = fmTextAlignCenter ' Center alignment
' .Height / .Width: Set the size of the control
.Height = 30
.Width = 250
End With
End Sub
Preparation Before Execution
- Create a UserForm named
DisplayForm. - Place a Label control on the form and change its (Name) to
TitleLabelin the Properties window. - Paste the
UserForm_Initializeprocedure above into the form’s code module. When you run the form, the label will appear with the applied design.
Explanation of the Code (Properties)
.Caption
This sets the actual text displayed on the label. You can dynamically change the string according to the situation, such as changing it to “Processing Complete”.
.Font
Font-related settings are configured through the .Font object.
- .Font.Name: Specify the font name installed on the PC (e.g., “Arial”, “Calibri”, “Times New Roman”) as a string.
- .Font.Bold: Set to
Truefor bold,Falsefor standard. - .Font.Size: Specify the size of the font in points.
.ForeColor and .BackColor
- .ForeColor: Sets the foreground color, which is the color of the text.
- .BackColor: Sets the background color. While you can use VBA constants like
vbRedorvbBlue, using theRGB(Red, Green, Blue)function allows for a wider variety of colors. Each value is specified in the range of 0 to 255.
.TextAlign
Sets the horizontal alignment of the text within the label.
fmTextAlignLeft: Left align (Default)fmTextAlignCenter: Center alignfmTextAlignRight: Right align
These values starting with fm... are VBA constants defined in the library.
Summary
In this article, I explained how to freely set the formatting of UserForm labels using VBA code.
- .Caption: Display Text
- .Font: Font type, weight, and size
- .ForeColor: Text Color
- .BackColor: Background Color
- .TextAlign: Horizontal Alignment
By combining these properties, you can transform a monotonous UserForm into a visually clear and well-designed interface. Please make use of these techniques to create user-friendly screens.
