Excel VBA UserForm Properties: A How-To Guide
In this article
To work with Excel VBA UserForm properties, open the Visual Basic Editor (Alt+F11), insert a UserForm, then select it and use the Properties window (F4) to change its name, caption, size, colors, and behavior. You can also set properties at runtime with code like UserForm1.Caption = "Data Entry". This guide walks through the practical steps, the properties that matter most, and how to avoid common pitfalls.
What You'll Need
- Excel with macros enabled (the Developer tab visible; if not, enable it via File > Options > Customize Ribbon).
- Basic familiarity with the Visual Basic Editor (VBE).
- A workbook saved as .xlsm (macro-enabled) to keep your UserForm code.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (or go to Developer > Visual Basic). The VBE opens in a separate window.
- Insert a UserForm. In the VBE menu, choose Insert > UserForm. A blank form appears, and the Properties window shows its default properties (Name: UserForm1, Caption: UserForm1). If the Properties window is not visible, press F4.
- Select the UserForm. Click anywhere on the form's background (not on a control) so the Properties window shows the form's properties, not a control's. The title bar of the Properties window should read "UserForm1 UserForm".
- Change the Name property. In the Properties window, scroll to (Name) and type a meaningful name, e.g.,
frmDataEntry. Avoid spaces and special characters. This name is what your VBA code will use. - Set the Caption. Find Caption and type the text you want in the form's title bar, e.g., "Data Entry". This is what users see.
- Adjust size and position. Use Height and Width to set dimensions in points. To center the form on screen, set StartUpPosition to 1 - CenterOwner (or 2 - CenterScreen). The default is 0 - Manual, which uses Left and Top values.
- Control appearance. BackColor and ForeColor accept color values; you can pick from the palette or enter a hex value like &H00FFFFFF& (white). BorderStyle offers 0 - None, 1 - Single, 3 - Fixed Dialog, etc. Keep 1 - Single for a standard look.
- Set behavior properties. ShowModal (True by default) blocks interaction with Excel until the form closes; set False for a non-modal form. Keep Modal but set ShowInTaskbar to False if you don't want a taskbar button. Use Zoom to scale the form (100 is normal).
- Add controls. From the Toolbox (View > Toolbox), drag TextBox, CommandButton, ComboBox, etc., onto the form. Each control has its own properties; select a control and edit its (Name), Caption, Text, and so on in the same Properties window.
- Write runtime code. In the UserForm's code module (right-click the form > View Code), you can set properties when the form loads. Example:
Private Sub UserForm_Initialize() Me.Caption = "Data Entry" Me.Width = 400 End Sub. UseMeto refer to the form itself. - Show the form. Insert a standard module (Insert > Module) and write
Sub ShowForm() frmDataEntry.Show End Sub. Run it with F5. To close from a button, useUnload Me. - Save and test. Save the workbook as .xlsm. Run the macro and verify the caption, size, and behavior match your settings. If you change properties in code, those override the design-time values at runtime.
Common Mistakes
- Editing the wrong object. Clicking a control instead of the form background shows the control's properties. Always confirm the Properties window header says "UserForm".
- Using spaces in Name. Names like
My Formcause compile errors. UsefrmMyForm. - Forgetting ShowModal. If the form seems to freeze Excel, it is likely modal (the default). Set ShowModal to False if you need to interact with sheets while it is open.
- Setting Caption instead of Text. For TextBox controls, the visible content is the Text property, not Caption (Caption does not exist on TextBox).
- Hard-coding positions. Manual Left/Top values can place the form off-screen on different monitors. Prefer StartUpPosition = 1 - CenterOwner.
- Not unloading. Closing with the X button hides the form but does not unload it unless you handle QueryClose. Use
Unload Mein your Close button.
Tips
- Use the Properties window's categorized view (click the Categorized tab) to find properties by group: Appearance, Behavior, Misc.
- Press F4 to toggle the Properties window; it is the fastest way to get back to form settings.
- Name controls with prefixes:
txtfor TextBox,cmdfor CommandButton,cbofor ComboBox. This makes code readable. - Set TabIndex on controls to control the tab order users experience.
- Use
Me.Controlsto loop through controls and set properties in bulk, e.g., clear all TextBoxes on reset. - Keep design-time properties for static settings and use runtime code only for dynamic changes (e.g., caption based on user role).
Troubleshooting
- Properties window is blank or missing: Press F4. If it still does not appear, go to View > Properties Window. Make sure you have selected the form or a control, not the code window.
- Form does not appear when running the macro: Check that the form name in
.Showmatches the (Name) property exactly. Also ensure the macro is in a standard module, not the form's code module. - Compile error: Expected: identifier: Usually a Name property with spaces or a reserved word. Rename the form and controls.
- Form appears tiny or huge: Check Zoom and the form's Height/Width. Also verify your display scaling; forms are sized in points, not pixels.
- Cannot click sheets while form is open: That is modal behavior. Set ShowModal to False in the Properties window or in code before
.Show. - Changes in code do not take effect: Design-time properties are overridden by runtime code. Search the form's code for assignments to the property you are changing.
Frequently Asked Questions
How do I open the Properties window in the VBE?
Press F4, or choose View > Properties Window. It shows properties for whatever is selected.
What is the difference between Name and Caption?
Name is the identifier used in code; Caption is the text users see in the title bar or on a button. Changing Caption does not affect code references.
Can I set UserForm properties without code?
Yes. Select the form and edit values in the Properties window at design time. Those become the defaults when the form is shown.
Why does my form block Excel?
Because ShowModal is True by default. Set it to False to allow interaction with the workbook while the form is open.
How do I center the form on screen?
Set StartUpPosition to 1 - CenterOwner (centers on the Excel window) or 2 - CenterScreen (centers on the monitor).
Can I change properties while the form is open?
Yes, from code. For example, Me.Caption = "Updated" in a button click event changes the title immediately.