To call a UserForm from a macro in Excel VBA, use the .Show method. The basic syntax is UserFormName.Show, where "UserFormName" is the actual name you gave the form in the Properties window. This command instructs Excel to load the form into memory and display it on the screen, pausing the rest of the macro's execution until the form is closed or hidden.
This process allows you to move beyond simple cell-based data entry and create a professional-looking interface. By using a UserForm, you can control exactly what data a user enters, provide dropdown menus for consistency, and prevent users from accidentally modifying your spreadsheet layout.
What You'll Need
- Microsoft Excel installed on a Windows PC (UserForms are not supported in Excel for Mac or Excel for the Web).
- The Developer tab enabled in the Excel Ribbon.
- An existing UserForm created within the Visual Basic Editor (VBE).
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This opens the VBE window where all your macros and forms are stored. - Identify Your UserForm Name: In the Project Explorer pane on the left, look under the "Forms" folder. Note the exact name of your form (e.g.,
UserForm1orfrmDataEntry). If you want to change it, click the form, go to the Properties window (F4), and edit the(Name)field. - Insert a Standard Module: You cannot call a form from within the form's own code to make it appear; you need a standard module. Go to the top menu and select
Insert > Module. - Write the Call Macro: In the new blank module window, type the following code:
Note: ReplaceSub OpenMyForm() UserForm1.Show End SubUserForm1with the actual name of your form. - Test the Macro: Click anywhere inside the
OpenMyFormsubroutine and pressF5, or return to your Excel sheet, pressAlt + F8, selectOpenMyFormfrom the list, and click Run. - (Optional) Assign the Macro to a Button: To make it user-friendly, go to the
Inserttab in the Excel Ribbon, choose a shape or a button from theIllustrationsorForm Controlsmenu, right-click the object, and select Assign Macro. ChooseOpenMyFormand click OK.
Common Mistakes
- Incorrect Naming: The most frequent error is a typo in the form name. VBA is not case-sensitive for these calls, but the spelling must be exact. If your form is named
frmInputand you typeUserForm1.Show, you will receive a "Variable not defined" or "Object required" error. - Calling from the Wrong Place: Trying to put the
.Showcommand inside theUserForm_Initializeevent. This creates a recursive loop where the form tries to show itself while it is still loading, which can cause Excel to crash. - Modal vs. Non-Modal Confusion: By default,
.Showopens a form as "Modal." This means the user cannot click on the spreadsheet or any other cell until the form is closed. Many users mistake this for Excel "freezing."
Tips
- Non-Modal Forms: If you want users to be able to interact with the spreadsheet while the form is still open, use
UserForm1.Show vbModeless. This is particularly useful for forms that act as toolbars or floating palettes. - Unloading vs. Hiding: Use
Unload Mein your "Close" button code to completely remove the form from memory. UsingMe.Hideonly makes the form invisible, meaning the data entered into the textboxes will remain there the next time the form is called. - Initialize Event: Use the
UserForm_Initializeevent within the form's own code window to set default values or populate ComboBoxes the moment the form is called.
Troubleshooting
If the UserForm does not appear or you receive an error message, try these steps in order:
- Check for Compile Errors: Go to the
Debugmenu in the VBE and selectCompile VBAProject. This will highlight any syntax errors in your code that are preventing the macro from running. - Verify the Form Name: Double-check the Project Explorer. Ensure the form hasn't been renamed or deleted.
- Check for "Break" Mode: If your code is highlighted in yellow, the macro is paused. Click the square Reset button on the toolbar to stop all execution before trying to run the macro again.
- Check Security Settings: Ensure that macros are enabled. Go to
File > Options > Trust Center > Trust Center Settings > Macro Settingsand ensure "Disable all macros with notification" is selected, and that you have clicked "Enable Content" on the yellow bar when opening the file.
Frequently Asked Questions
Can I pass data from a cell into the UserForm when I call it?
Yes. You can assign the cell value to a public variable or a specific control on the form before calling the .Show method. For example: UserForm1.TextBox1.Value = Range("A1").Value followed by UserForm1.Show.
Why can't I click on my Excel cells while the UserForm is open?
This is because the form is "Modal" by default. To fix this, change your code to UserForm1.Show vbModeless.
How do I make the UserForm open automatically when the workbook opens?
In the Project Explorer, double-click ThisWorkbook and paste the UserForm1.Show command inside the Private Sub Workbook_Open() event.
Does this work on Excel for Mac?
No. UserForms are a feature of the Windows-only VBA library. While Mac supports basic VBA macros, it does not support the creation or display of UserForms.
What is the difference between .Hide and Unload?
.Hide makes the form invisible but keeps it in memory, preserving any data the user typed. Unload completely removes the form and its data from the computer's RAM.