To change an Excel VBA UserForm’s background color, set its BackColor property: in the form’s code module, use Me.BackColor = RGB(240, 248, 255). You can also set BackColor in the Properties window at design time, without writing code.
What the BackColor property changes
A UserForm is a custom dialog box created with Excel’s Visual Basic for Applications editor. Its BackColor property sets the background of the form’s main client area—the space behind the controls you place on it. It does not automatically recolor labels, text boxes, buttons, or other controls. Each control has its own appearance properties, including BackColor where supported.
For example, you might use a pale blue background for an input form while keeping text boxes white so that the places where users type remain easy to identify. Setting the form’s color does not change the worksheet, workbook theme, or Excel application window. It applies to that UserForm.
BackColor accepts a color value. A convenient way to create one in VBA is the RGB function, which takes red, green, and blue components from 0 through 255. The form can be given the color when it is designed, or code can assign a different value when it opens or while it is being used.
Set the color in the Properties window
For a fixed color that should be the same every time, set the property in the Visual Basic Editor (VBE). In Excel for Microsoft 365 on Windows, open the Developer tab and choose Visual Basic. If Developer is not visible, enable it through File > Options > Customize Ribbon and select Developer. In the VBE, choose Insert > UserForm if you have not made the form yet.
- Select the UserForm itself by clicking an empty area of its surface. Selecting a control instead will show that control’s properties.
- Open the Properties window with View > Properties Window, or press F4.
- Find
BackColorin the property list. Click its value to open the color selector, then choose a color. - Run the form to check the result and confirm that the controls remain readable against the new background.
This method is useful when the form has one permanent design color. It also makes the color visible in the VBE without requiring a macro to assign it. A code assignment during initialization is better if the color depends on a setting, user choice, or other information that is not known until the form opens.
Set the background in VBA code
To assign a color as the form opens, put a UserForm_Initialize event procedure in that form’s own code module. In the VBE, right-click the form and choose View Code, or double-click the form surface. From the left procedure dropdown select UserForm; from the right dropdown select Initialize. The editor creates the event procedure in the correct module.
Private Sub UserForm_Initialize()
Dim backgroundColor As Long
' Use a pale blue background when the form initializes.
backgroundColor = RGB(240, 248, 255)
Me.BackColor = backgroundColor
End Sub
Me refers to the UserForm whose code is running, so Me.BackColor is generally a clear choice inside that form’s module. The example’s RGB values are red 240, green 248, and blue 255, producing a very light blue. The initialization event runs when the form is initialized, before it is displayed for the first time in the usual load-and-show sequence.
You can also set the property from another procedure after loading the form, using its object name, such as UserForm1.BackColor = RGB(240, 248, 255). In that case, make sure the form has been loaded before referring to it, and remember that code outside the form module needs the correct form name. For a form-specific appearance, keeping the assignment in its own initialization procedure is often easier to maintain.
Choose colors with RGB
RGB uses three whole-number components in this order: red, green, and blue. Each component ranges from 0 to 255. Zero for all three produces black; 255 for all three produces white. Increasing one component makes that color channel stronger. For instance, RGB(255, 0, 0) is red, while RGB(0, 0, 255) is blue.
Here are a few illustrative choices you can try in the assignment:
RGB(255, 255, 255)for white.RGB(245, 245, 245)for a light gray.RGB(240, 248, 255)for a pale blue.RGB(255, 255, 0)for bright yellow.
Use values that keep the form’s text and controls legible. VBA can also represent colors with hexadecimal or named constants in some contexts, but RGB is often easier to read and adjust because its three arguments correspond directly to the color channels. Do not reverse the argument order: VBA’s RGB function expects red first, then green, then blue.
Change the color while the form is open
A form can change color in response to an event, such as a button click. Add a CommandButton to the form, set its Name property to cmdBlue, and place the following procedure in the same UserForm code module. The button’s Click event changes the form’s background when the user selects it.
Private Sub cmdBlue_Click()
Dim newColor As Long
' Switch the form background to blue.
newColor = RGB(210, 230, 255)
Me.BackColor = newColor
End Sub
To create the event procedure, double-click the button in the VBE. If the button has a different Name, use that name in the procedure declaration. This technique can support a simple theme selector, but a color-selection interface requires additional controls and code to capture the user’s choice. For a form that only needs a consistent background, a fixed design-time value or initialization assignment is simpler.
Color the controls separately
A common surprise is that changing Me.BackColor leaves labels or other controls in their previous colors. That is expected: the form and its controls have separate properties. A control’s BackColor can be assigned in the Properties window or in code, for example by referring to a control by its Name. The exact visual effect depends on the type of control and its other properties.
For instance, if a text box appears with a different background, set that text box’s BackColor separately rather than repeatedly changing the form. Some controls also have a BackStyle setting that determines whether their background is opaque or transparent. A transparent label can show the form behind it; an opaque label may cover the form color with its own background. If a control does not show the color you expect, inspect its own properties first.
There is no requirement that every control match the form. Often a restrained contrast works better: a neutral form background, white entry fields, and clear labels. Keep related controls visually consistent, and check the form at its actual size rather than judging only the VBE design surface.
Common problems and how to fix them
- The color does not change: Confirm that the code is in the intended UserForm module and that the procedure is actually run. A line placed in a standard module will not run automatically as a UserForm initialization event.
- The form is colored but a control is not: Set that control’s own BackColor or review its BackStyle. The form’s BackColor does not cascade to all controls.
- The code reports an error: Check spelling, parentheses, and the three RGB arguments. Each component must be a number from 0 to 255. Also confirm that the control name in an event procedure matches the button’s Name property.
- The chosen color looks different than expected: Verify the RGB order—red, green, blue—and test with a familiar color such as white or black. A subtle RGB difference can be difficult to notice on screen.
- Changes disappear after reopening the workbook: If the color is assigned only by code, confirm the event runs when the form is shown. If you edited VBA, save the workbook afterward.
When the displayed form does not refresh immediately after a property change, check whether your code is changing the intended form instance and whether the assignment has executed. Repaint can request a redraw in some situations, but it is not a substitute for correcting the event or object reference.
Design, readability, and compatibility
Color is a visual aid, not a substitute for clear labels and instructions. Avoid low contrast between text and its background, and do not use color alone to communicate an error or required action. For example, pair a warning color with explanatory text. Users may have different display settings or color-vision needs, so a restrained palette is usually more practical than a bright full-form fill.
The VBA UserForm approach described here is for desktop Excel. Excel for the web can open and edit many workbook contents, but it does not run VBA macros, so a VBA UserForm will not operate there. Excel for Mac supports VBA, but the VBE interface and keyboard shortcuts can vary by keyboard and version; on many Mac keyboards, opening the VBE uses Option+F11, sometimes with Fn as well. The form property and RGB assignment are VBA concepts, though Windows and Mac can render interface colors somewhat differently.
Before relying on a macro-enabled workbook, confirm that recipients can use desktop Excel and that their macro security settings permit the code to run. If you edit or run a macro, save a copy of the workbook first: VBA changes cannot be undone with Ctrl+Z. Save a workbook containing macros in a macro-enabled format such as .xlsm, not as a macro-free .xlsx file if you need to retain its VBA project.
Frequently Asked Questions
How do I change a UserForm background color without VBA code?
Open the form in the VBE, select an empty part of the form, open the Properties window, and choose a value for BackColor. That sets a design-time color that remains part of the form.
What is the VBA code for a UserForm background color?
Inside the UserForm’s code module, use an assignment such as Me.BackColor = RGB(240, 248, 255). Put it in UserForm_Initialize if the color should be applied as the form initializes.
Why does changing BackColor not change my labels or text boxes?
BackColor belongs to each object. The UserForm’s BackColor changes the form surface, while controls can retain their own colors and background styles. Set the relevant control properties separately if needed.
Can I use a color name such as blue instead of RGB?
For a straightforward, readable assignment, use VBA’s RGB function, for example RGB(0, 0, 255) for blue. A plain word such as blue is not, by itself, an RGB color value in that assignment.
Will a VBA UserForm work in Excel for the web?
No. Excel for the web does not run VBA macros or display VBA UserForms as working interactive forms. Use desktop Excel for a VBA-based form, or consider a web-compatible alternative if users must work in a browser.
Related
- Excel Trendlines with Dates: How to Make Them Work
- Excel Text to Columns Shortcut Key for Windows and Mac
- Excel Text to Columns Using Enter: Split Line Breaks
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Copy and Paste a Dynamic Range AutomaticallyTo copy a range that grows or shrinks, use VBA to find its last populated row and column,…
- Excel VBA Integer: Range, Limits, and Type ChoiceIn Excel VBA, Integer is a signed whole-number data type that stores values from -32,768 through 32,767. It…
- Excel VBA Filter Range: AutoFilter Examples and FixesTo filter a range with Excel VBA, use the range’s AutoFilter method and specify the field number and…
- VBA Find a Plumber in an Excel Contact ListTo find plumber listings in an Excel contact list with VBA, search the column that contains service categories…
- VBA Find First Empty Cell in a ColumnTo find the first truly empty cell in column A with VBA, scan from the top and test…
- Excel VBA: Copy a Worksheet to Another Workbook SafelyTo copy a worksheet from one open workbook to another in Excel VBA, use the worksheet’s Copy method…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.