Excel VBA UserForm Initialize: Step-by-Step Guide
In this article
To initialize a UserForm in Excel VBA, open the form's code module (right-click the form in the Project Explorer and choose View Code), select UserForm from the left dropdown and Initialize from the right dropdown, then add your setup code inside the Private Sub UserForm_Initialize() block. That code runs automatically when the form is loaded into memory, before it becomes visible. This guide walks through the exact steps, what to put in the handler, and how to avoid the most common traps.
What You'll Need
- Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer; on Mac, Excel > Preferences > Ribbon & Toolbar).
- A workbook saved as .xlsm (macro-enabled) or .xlsb, because standard .xlsx files cannot store macros.
- Macro security set to allow macros (Developer > Macro Security). If macros are disabled, the form will not load and the Initialize event will never fire.
- An existing UserForm, or a few minutes to insert one (Developer > Visual Basic > Insert > UserForm).
Step-by-Step
- Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab > Visual Basic). The editor opens with the Project Explorer on the left. If it is hidden, press Ctrl+R.
- Insert or locate your UserForm. In the Project Explorer, expand your workbook's VBAProject node, right-click and choose Insert > UserForm. A blank form appears in the design area and is added under the Forms folder.
- Add the controls you need. Use the Toolbox (View > Toolbox) to drag a ComboBox, TextBox, ListBox, or CommandButton onto the form. Give each control a meaningful Name in the Properties window (F4), for example cboDepartment or txtCustomerName. Names matter because your Initialize code will refer to them.
- Open the form's code module. Right-click the form's name in the Project Explorer and choose View Code, or double-click the form and then press F7. A blank code window opens for that form.
- Create the Initialize event handler. At the top of the code window, use the left dropdown to select UserForm and the right dropdown to select Initialize. The editor inserts:
Private Sub UserForm_Initialize()End Sub
If you prefer, type it manually exactly as shown. - Add your setup code between the Sub and End Sub lines. Typical tasks include populating a ComboBox with a list, setting default text, disabling a button until fields are filled, or loading values from a worksheet. For example:
Private Sub UserForm_Initialize()Me.Caption = "Customer Entry"cboDepartment.AddItem "Sales"cboDepartment.AddItem "Support"cboDepartment.ListIndex = 0txtCustomerName.Value = ""cmdSave.Enabled = FalseEnd Sub - Test the form. Press F5 while the form is active in the editor, or run a macro that calls UserForm1.Show. The Initialize code runs first, then the form appears with your defaults already applied.
- Use Initialize for data loading, not for showing the form. Do not call UserForm1.Show inside Initialize; it can cause recursion or unexpected behavior. If you need to load data from a sheet, read it into variables or control properties here, but leave the display to the calling macro.
- Keep the handler short and defensive. Wrap worksheet reads in error handling or check that the sheet exists, because an unhandled error in Initialize prevents the form from opening at all. A simple On Error GoTo label with a message box is enough.
- Save and close. Save the workbook as .xlsm, close the form with the X, and reopen it from your macro to confirm the defaults persist.
Common Mistakes
- Putting code in the wrong event. UserForm_Activate fires every time the form gets focus, so defaults set there can reset user input. Use Initialize for one-time setup and Activate only for things that must refresh on each activation.
- Referring to controls that do not exist. If you rename a control after writing code, the old name causes a compile error. Use the dropdowns in the code window to insert correct names, or enable Option Explicit (Tools > Options > Require Variable Declaration) to catch typos.
- Assuming Initialize runs on every Show. If the form is already loaded (for example, hidden with Me.Hide instead of unloaded), Show does not fire Initialize again. Unload the form (Unload Me) when you want a fresh start.
- Forgetting macro security. If macros are disabled, the form will not open and no event code runs. Check the Trust Center settings and the yellow security bar.
- Using Initialize to validate user input. At that point the user has not typed anything, so validation belongs in the OK button's Click event or in the control's AfterUpdate event.
Tips
- Use Me to refer to the form itself; Me.Caption, Me.Controls, and Me.Hide are clearer than the form's name and survive renaming.
- Load lists from a worksheet range with a loop or Application.WorksheetFunction.Transpose, but read the range into a Variant array first for speed on large lists.
- Set TabIndex in the Properties window so the cursor starts in the first field; Initialize can also call txtCustomerName.SetFocus, but focus calls can fail if the form is not yet visible, so prefer TabIndex.
- Keep a clear naming convention (cbo, txt, lst, cmd) so Initialize code reads like a checklist.
- Comment each block in Initialize; six months later you will not remember why a default was set.
Troubleshooting
The form opens but defaults are missing. Check that the code is in the UserForm_Initialize procedure, not in a standard module or the form's Activate event. Also confirm the form was unloaded before showing again.
Compile error: Method or data member not found. A control name in your code does not match the form. Open the Properties window, compare names, and use the dropdowns in the code window to insert the correct reference.
Runtime error when reading a worksheet. The sheet name may be wrong or the workbook may not be active. Qualify references with ThisWorkbook.Worksheets("Sheet1") and add a check that the sheet exists.
The form does not appear at all. Macro security is the usual cause. Enable macros for the file's location, or add the folder to Trusted Locations. If the workbook is .xlsx, save it as .xlsm first.
Initialize runs twice. Something is showing the form twice, or the form is being reloaded after Unload. Search your code for duplicate UserForm1.Show calls and remove them.
Frequently Asked Questions
What is the difference between UserForm_Initialize and UserForm_Activate?
Initialize runs once when the form is loaded into memory. Activate runs each time the form becomes active, including after switching back to it, so it can fire multiple times.
Can I pass arguments to a UserForm_Initialize?
Not directly. Initialize takes no arguments. Set public variables or properties on the form before calling Show, then read them inside Initialize.
Why does my ComboBox stay empty after Initialize?
Check that you used AddItem or set RowSource correctly, that the control name matches, and that the code is in the form's module. If RowSource points to a sheet, the sheet must exist and the range must be valid.
Does Initialize run when the form is hidden with Me.Hide?
No. Hide only makes the loaded form invisible. To trigger Initialize again, unload the form with Unload Me or Unload UserForm1 before showing it.
How do I set a default value for a TextBox?
In Initialize, assign txtName.Value = "Default". The user can overwrite it, and the value resets only when the form is loaded again.
Can I call another macro from Initialize?
Yes, as long as the macro is in a standard module and does not try to show the same form. Keep such calls short and handle errors so a failure does not block the form from opening.