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

  1. Open the Visual Basic Editor: Press Alt + F11 on your keyboard. This opens the VBE window where all your macros and forms are stored.
  2. 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., UserForm1 or frmDataEntry). If you want to change it, click the form, go to the Properties window (F4), and edit the (Name) field.
  3. 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.
  4. Write the Call Macro: In the new blank module window, type the following code:
    Sub OpenMyForm()
        UserForm1.Show
    End Sub
    Note: Replace UserForm1 with the actual name of your form.
  5. Test the Macro: Click anywhere inside the OpenMyForm subroutine and press F5, or return to your Excel sheet, press Alt + F8, select OpenMyForm from the list, and click Run.
  6. (Optional) Assign the Macro to a Button: To make it user-friendly, go to the Insert tab in the Excel Ribbon, choose a shape or a button from the Illustrations or Form Controls menu, right-click the object, and select Assign Macro. Choose OpenMyForm and 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 frmInput and you type UserForm1.Show, you will receive a "Variable not defined" or "Object required" error.
  • Calling from the Wrong Place: Trying to put the .Show command inside the UserForm_Initialize event. 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, .Show opens 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 Me in your "Close" button code to completely remove the form from memory. Using Me.Hide only 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_Initialize event 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:

  1. Check for Compile Errors: Go to the Debug menu in the VBE and select Compile VBAProject. This will highlight any syntax errors in your code that are preventing the macro from running.
  2. Verify the Form Name: Double-check the Project Explorer. Ensure the form hasn't been renamed or deleted.
  3. 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.
  4. Check Security Settings: Ensure that macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and 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.