VBA: Close a UserForm with a Command Button

VBA & macros · Updated November 18, 2026

To close an Excel VBA UserForm when someone clicks a command button, put Unload Me in that button’s Click event procedure. Use Me.Hide instead if you want the form to disappear but remain loaded so you can read its values or show it again.

The simplest way to close a UserForm

In the Visual Basic Editor, open the UserForm’s code module and place the close instruction in the command button’s Click event. If the button is named cmdClose, the procedure is usually named cmdClose_Click:

Private Sub cmdClose_Click()
    ' Close and unload this instance of the UserForm.
    Unload Me
End Sub

Here, Me means the particular UserForm instance whose code is running. That makes it a reliable choice inside the form’s own event procedure: the button closes its own form, rather than relying on a form name that might refer to a different instance.

The button’s name matters. If its Name property is still CommandButton1, the event procedure must be CommandButton1_Click, unless you rename the control. The button’s visible text is controlled separately by its Caption property; changing the caption to “Close” does not rename the control or change the event procedure.

Add the button and create its Click event

In desktop Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. In the editor, select Insert > UserForm if you need a new form. Show the Toolbox if it is not visible, add a CommandButton, and use the Properties window to set its Name to cmdClose and its Caption to Close. Double-click the button to create or open its Click event procedure, then enter the code.

Keep this event code in the UserForm’s code module. A procedure in a standard module is not the button’s Click event, and Me is not a reference to a form in a standard module. Double-clicking the control is a convenient way to reach the correct event; you can also select the control and its Click event in the code window’s object and procedure lists.

On Excel for Mac, VBA UserForms and the Visual Basic Editor are available in desktop Excel, though keyboard shortcuts and editor navigation can differ; use the Excel or Visual Basic Editor menus if a Windows shortcut does not work. Excel for the web does not run VBA macros, so a UserForm close button will not operate there. Save the workbook in a macro-enabled format such as .xlsm if it contains VBA.

Unload versus Hide

Unload Me closes the current form and removes that instance from memory. Its controls and their current values are no longer available from that instance after it has been unloaded. If the form is shown again, VBA creates or loads an instance again, and code that depends on prefilled values should initialize them again as needed.

Me.Hide makes the form invisible but keeps the instance loaded. The text in text boxes, selected options, and other control values remain available while that instance exists. This is useful when the form is a dialog that gathers information and the calling procedure needs to inspect the controls after the user clicks a button.

Choose according to what should happen to the form’s state:

  • Use Unload Me for a straightforward close when the form’s current control values are no longer needed.
  • Use Me.Hide when the form should be temporarily dismissed or the caller still needs to read its entered values.
  • Use Hide only when something else will eventually unload the form, such as the procedure that showed it or a later cleanup step.

Hiding is not the same as closing and releasing an instance. A hidden form can remain in memory and retain data, so repeatedly hiding forms without unloading them can leave unwanted instances or stale values around.

What happens when the form is shown

A UserForm shown with vbModal (the default if you omit the mode) prevents the user from interacting with the rest of the Excel application until the form is hidden or unloaded. The VBA procedure that called Show pauses while the modal form is displayed. When the user clicks a button that runs Me.Hide or Unload Me, execution can continue after the Show statement.

That distinction is useful for data-entry forms. If the caller needs to examine control values after the form closes, it can show a modal form, let the user click a button that hides it, then read the values before unloading it. With a modeless form, Excel and the calling code are not blocked in the same way; code after Show vbModeless runs without waiting for the user to close the form.

For a form created with a variable, keep track of that instance. For example, a variable set to New UserForm1 refers to that created instance, and code should show, read, and unload the same instance. Inside its button event, Me still identifies the correct form. This is one reason to prefer Unload Me within the form instead of writing Unload UserForm1, which may target the default instance rather than another instance you created.

Closing with the window’s X button

The UserForm title bar’s X is a separate way to close the form. Clicking a command button runs that button’s Click event; clicking the X follows the form’s close behavior and can be handled by the form’s QueryClose event. If you need to prevent a user from dismissing the form with the X, or need to ask for confirmation there, handle that event deliberately. A command-button Click handler alone does not control the X button.

For many forms, the default X behavior is perfectly adequate. If the X should have the same effect as your Close button, you generally do not need extra code just to close the form. Add a QueryClose handler only when you have a real requirement, such as warning about unsaved entries. If you cancel a close in QueryClose, make sure the user has another clear way to leave the form; otherwise the form can become difficult to dismiss.

You can also set a CommandButton’s Cancel property to True in the Properties window. This makes the button respond to the Escape key when the form is active, by running that button’s Click event. It does not intercept the title-bar X. The Default property similarly makes a button respond to Enter in applicable form situations, so use it carefully if Enter should submit rather than close.

Confirmation and validation before closing

A close button does not have to unload the form immediately. For a form with unsaved or important entries, you can ask for confirmation before closing. This example uses a declared variable for the message-box result:

Private Sub cmdClose_Click()
    Dim response As VbMsgBoxResult

    ' Close only after the user confirms.
    response = MsgBox("Close this form?", vbQuestion + vbYesNo, "Confirm")
    If response = vbYes Then
        Unload Me
    End If
End Sub

You can use the same structure for validation, but put the validation checks before Unload Me. For example, if a required text box is empty, display a message and exit the event procedure instead of closing. Keep the validation rule specific to the data the form collects; a generic close handler should not silently discard entries without warning when those entries matter.

Common problems and how to fix them

  • The button does nothing: Confirm that the code is in the UserForm module and that the procedure name matches the control’s Name property. A procedure for cmdClose must be cmdClose_Click.
  • “Me” is not accepted: The code is probably in a standard module or a worksheet module instead of the UserForm’s code module. Move the handler to the form, or use an appropriate reference to the form instance from outside it.
  • The form disappears but values are needed afterward: Replace Unload Me with Me.Hide, read the controls from the caller, then unload the form when finished.
  • A different form closes: Check whether the project uses more than one instance. Unload UserForm1 refers to the default instance by name, while Me refers to the instance running the event.
  • The macro does not run: Check that macros are permitted for the workbook and that it is opened in desktop Excel. VBA is not executed by Excel for the web.

When debugging, place the cursor in the Click procedure and use the editor’s run or step-through tools, or add a breakpoint to see whether the event fires. If the event fires but the form remains visible, inspect the actual code path for conditions that skip the unload instruction. Remember that VBA macros cannot be undone with Ctrl+Z; save a copy of the workbook before running or changing a macro that could affect important data.

Frequently Asked Questions

What code closes a UserForm in VBA?
In the UserForm’s own command button Click event, use Unload Me. It unloads the form instance whose button was clicked.

Should I use Unload Me or Me.Hide?
Use Unload Me when you are finished with that instance. Use Me.Hide when you need the form’s entered values afterward or intend to show the same loaded instance again.

Can I close a UserForm with a button named Close?
Yes. Set the button’s Caption to Close and put the unload instruction in its Click event. The Caption is just the displayed label; the event procedure is determined by the button’s Name.

How do I close a UserForm from a standard module?
Refer to the form instance you actually showed and unload that instance. If you use the default instance, a statement such as Unload UserForm1 can target it, but it may not be the right instance when your code created another one. Inside the form’s own event, Unload Me avoids that ambiguity.

Does Unload Me also close the form when I click its X?
The X follows the form’s normal close behavior, not the command button’s Click event. Use the UserForm’s QueryClose event if you need to customize or cancel that route.

Can I use a VBA UserForm close button in Excel for the web?
No. Excel for the web does not run VBA macros. Open the macro-enabled workbook in desktop Excel to use its VBA UserForm and button events.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.