Excel VBA UserForm: How to Set Properties
In this article
You set UserForm properties in the VBA Properties window (F4) at design time, or in code at run time with Me.ControlName.Property = Value. Design-time changes are saved with the form; run-time changes last only while the form is open. The steps below cover both, plus the properties that most often trip people up.
What You'll Need
- Excel on Windows with the Developer tab enabled (File > Options > Customize Ribbon > check Developer). The VBA editor and UserForm designer are not available in Excel for the web or Excel for Mac in the same form; Mac Excel has a limited VBA editor and no UserForm designer, so use a Windows PC.
- A workbook saved as .xlsm (macro-enabled) so your form and code persist.
- Macro security set to allow the file: File > Options > Trust Center > Trust Center Settings > Macro Settings, or store the workbook in a Trusted Location.
- An existing UserForm, or two minutes to insert one: in the VBA editor, Insert > UserForm.
Step-by-Step
- Open the VBA editor. Press Alt+F11, or use Developer > Visual Basic. Menu labels can differ slightly by Excel version and region.
- Show the form. In the Project Explorer (Ctrl+R), expand your workbook, expand Forms, and double-click the UserForm. The designer opens and the Properties window (F4) lists every property of the selected object.
- Select the right object first. Click the form's background to edit form properties, or click a control (text box, button, list box) to edit that control's properties. The Properties window's dropdown at the top shows which object you are editing. This is the single most common source of confusion: changing a property and seeing no effect usually means the wrong object was selected.
- Set the obvious form-level properties in the Properties window. With the form selected, set Name to something meaningful (for example
frmEntry), Caption to the title bar text, and Width and Height to size it. Set StartUpPosition to 1 - CenterOwner (centres over Excel) or 2 - CenterScreen. Set ShowModal to True if the user must deal with the form before returning to the sheet. - Set control-level properties. Click a text box and set Name (for example
txtCustomer), MaxLength, TextAlign, and TabIndex to control tab order. For a command button, set Caption and Default (True makes Enter trigger it) or Cancel (True makes Esc trigger it). For a list box, set ColumnCount and ColumnWidths. - Use the Properties window's grouped tabs. Properties are sorted alphabetically; the tabs at the bottom switch between Alphabetic and Categorized views. Categorized is easier when you are learning because it groups Appearance, Behavior, Font, Misc and so on.
- Set properties in code when they must change at run time. Right-click the form in the Project Explorer and choose View Code, then use the
UserForm_Initializeevent. Inside the form's own code module,Merefers to the form, soMe.Caption = "Enter Order"andMe.txtCustomer.Value = ""work directly. From a normal module, use the form's name:frmEntry.Caption = "Enter Order". - Know which properties are design-time only. Some properties, such as Name, cannot be changed in code; attempting it raises an error. Others, such as Visible, Enabled, Caption, Value, BackColor and ListIndex, are routinely set at run time.
- Show the form and test. From a standard module, run a short macro such as
Sub ShowIt()/frmEntry.Show/End Sub. Press F5 with the cursor inside the sub. Confirm the caption, sizes and control settings appear as intended. - Save and re-test from a fresh open. Close the workbook completely, reopen it, and run the macro again. This catches settings that only worked because the form was already loaded in the editor.
Common Mistakes
- Editing the wrong object. The Properties window always reflects the current selection. Click the form background, not a control, when setting form properties.
- Confusing Name and Caption. Name is what your code uses; Caption is what the user sees. Renaming a control after writing code breaks every reference to the old name.
- Setting properties after
Showreturns. Code placed afterfrmEntry.Showruns only when the form closes, so run-time property changes there have no visible effect. - Expecting design-time changes to survive a run-time change. Run-time values reset to the design-time defaults the next time the form loads.
- Using
Unload Methen touching the form. After unloading, the form's controls are gone; set values before unloading or useMe.Hideif you need to reuse the form. - Trying to change Name in code. It is read-only at run time and will error.
Tips
- Use a consistent naming prefix (
txt,cbo,lst,cmd,frm) so the Properties window and your code stay readable. - Set TabIndex in the order a user would fill the form; it is faster than dragging controls around.
- Set ControlTipText on each input to give users a hover hint without adding labels.
- Keep all run-time setup in
UserForm_Initializeso the form always opens in a known state. - Use
Me.Controlsin a loop to apply the same setting to many controls at once, for example clearing every text box. - If a form grows complex, set the important properties in code rather than the designer, so the behaviour is visible in one place.
Troubleshooting
A property change does nothing. Check the object dropdown at the top of the Properties window, and confirm you are not looking at a run-time value that resets on load.
"Compile error: Method or data member not found". The control name in your code does not match the Name property. Click the control and read the name in the Properties window, then correct the code.
The form opens in the wrong place or size. Set StartUpPosition and Width/Height on the form itself, not on a control, and re-test after a full close and reopen.
Macros are blocked. Check Trust Center macro settings or move the workbook to a Trusted Location. Do not lower security settings for files from unknown sources.
The form does not appear at all. Confirm the macro actually ran (Developer > Macros), and that the sub calls .Show. If the form is modal and already open, a second .Show call is ignored.
Frequently Asked Questions
Can I change a UserForm property in code?
Yes, for most properties, using Me.PropertyName inside the form's code or FormName.PropertyName from a module. Name is the main exception and is design-time only.
Where is the Properties window?
Press F4 in the VBA editor, or choose View > Properties Window. It appears automatically when a form or control is selected.
Do run-time property changes get saved?
No. They apply only while the form is loaded. To make a change permanent, set it in the designer and save the workbook.
Why does my form ignore the Caption I set?
You likely set it on a control rather than the form, or set it in code after the form was already shown. Select the form background and check the Properties window.
Can I use UserForms in Excel for Mac or the web?
The UserForm designer is a Windows VBA feature. Excel for the web has no VBA, and Mac Excel's VBA support does not include the UserForm designer, so build and test forms on Windows.
How do I reset a form to its original state?
Close it with Unload Me and show it again; the designer's saved values reload. Alternatively, explicitly reset each control in UserForm_Initialize.