To quit a VBA UserForm, use Unload Me in a button or event procedure on the form; this closes the form and removes its current instance. Use Me.Hide instead if you want to dismiss the form but keep its values available for later. Neither command quits Excel.
What “quit” means for a UserForm
VBA does not have a UserForm.Close method. The usual ways to make a form disappear are Hide and Unload, and they have different effects on the form’s lifetime. Choosing between them is the key to avoiding lost values or unexpected old values when a form is shown again.
A UserForm is a VBA object containing controls such as text boxes, buttons, and list boxes. Hiding the object changes whether it is displayed; unloading it ends the current loaded instance. Closing a UserForm is therefore not the same as closing the workbook, stopping a macro, or exiting Excel.
Me.Hideconceals the current form but keeps it loaded.Unload Mecloses the current form and unloads it.Application.Quitexits the Excel application, not just the form, and is usually not what a form’s Close button should do.
Close a form from its own button
For a form button named cmdQuit, double-click the button in the Visual Basic Editor to open its click event, then use Unload Me. Here, Me means the UserForm whose code is running, so the same event code works even if you later rename the form.
Private Sub cmdQuit_Click()
' Close this form and unload its current instance.
Unload Me
End Sub
To add this code in Excel for Microsoft 365 on Windows, open the workbook, press Alt+F11, find the form in Project Explorer, and open it. If Project Explorer is not visible, use View > Project Explorer. Add a CommandButton from the Toolbox if the form does not already have one, then double-click that button and enter the procedure. On a Mac, the Visual Basic Editor is available through the Developer tab; keyboard shortcuts can differ by keyboard and Excel settings.
Before running a macro that changes workbook data, save a copy of the workbook. VBA actions cannot be undone with Ctrl+Z, even if the form itself is easy to reopen.
Choose between Hide and Unload
Use Hide when the calling procedure needs to read the form’s controls after the user dismisses it. For example, a procedure can show a form, let the user enter a name, hide it when the user clicks OK, and then read the text box. The form remains loaded, so the text box still contains its entry.
Use Unload when the form is finished and you do not need to retain its control values. Unloading is useful for a Cancel or Quit button, or after the caller has copied the entered values somewhere safe. When a new instance is loaded, its controls are initialized again; initialization code can also set their starting values.
- OK or Continue: Often hide the form, then let the calling procedure validate and use the entries.
- Cancel without retaining entries: Unload the form if the values should be discarded.
- Temporarily dismiss and return later: Hide it, provided the code that owns the form will show the same instance again.
One common misconception is that Hide destroys the form. It does not. Another is that Unload automatically saves control values to the worksheet. It does not: your code must copy values to cells, variables, or another destination before unloading if they need to be kept.
How Show and the calling procedure interact
A UserForm is commonly displayed with UserForm1.Show. By default, the form is modal: the code that called Show pauses until the form is hidden or unloaded. This makes Hide a convenient way to return control to the procedure while leaving the form’s controls available to inspect.
For example, with a form named UserForm1 and a text box named txtName, the following pattern shows a modal form, reads the text after the user clicks an OK button that hides the form, and then unloads it. The example assumes the OK button uses Me.Hide.
Public Sub CollectName()
Dim enteredName As String
' Show the form modally and wait for it to be hidden.
UserForm1.Show vbModal
enteredName = UserForm1.txtName.Value
' The value has been copied, so the form can be unloaded.
Unload UserForm1
End Sub
With a modeless form, shown using UserForm1.Show vbModeless, the calling code continues immediately instead of waiting. Do not put code directly after a modeless Show and assume the user has finished entering data. In that design, use an event-driven approach: for example, the form’s OK button can validate and process the entry before unloading the form.
Handle the X button and cancellation
Users may close a form with its title-bar X rather than a button. The UserForm_QueryClose event lets you respond before that close takes effect. Its CloseMode argument identifies the way the close was requested; vbFormControlMenu indicates the form’s control menu, including the title-bar close button.
For example, you can prevent the X from closing a form and require a dedicated Cancel button. This sample declares the event arguments as required by the event procedure and cancels only the title-bar close. Use this intentionally: if users cannot close the window, provide a clear alternative.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' Keep the form open when the title-bar close button is clicked.
If CloseMode = vbFormControlMenu Then
Cancel = True
End If
End Sub
Setting Cancel to True prevents the close request. If you do not cancel it, the form closes normally. The event can also run when code unloads the form, with CloseMode set to vbFormCode. Avoid adding complicated cleanup or showing another modal form inside this event; simple decisions and validation are easier to maintain.
Default instances and explicitly created forms
In Excel VBA, a form can be referenced by its default instance, such as UserForm1. That is why examples using UserForm1.Show and Unload UserForm1 are common. For a small workbook with one form at a time, this approach is straightforward.
If your code creates a separate form object with New, keep track of that object and unload that instance—not a different form reference. For example, a modal form can be created and managed like this:
Public Sub ShowEntryForm()
Dim frm As UserForm1
' Create and show a separate form instance.
Set frm = New UserForm1
frm.Show vbModal
' Release the instance after the modal form returns.
Unload frm
Set frm = Nothing
End Sub
This example assumes the form is hidden or unloaded by its own interface before the modal Show returns. It is useful when separate instances are needed, but do not mix default-instance references and explicitly created instances casually: the displayed form may not be the one your later code reads or unloads.
Common problems and safer patterns
- The form vanishes, but values are still needed: Replace
Unload MewithMe.Hide, then read the controls in the waiting caller before unloading. - Old values appear the next time: The form was probably hidden and reused. Clear or reset controls deliberately, or unload the form when finished.
- Code after Show does not run: A modal form is still open. Close or hide it to let the calling procedure continue.
- Code after a modeless Show runs too soon: Modeless display does not pause the caller. Move follow-up work into an appropriate form event.
- A button closes the wrong form: Use
Unload Meinside that form’s own code, or confirm that the reference being unloaded is the actual instance shown.
Do not use the VBA End statement as a substitute for closing a UserForm. End stops VBA execution abruptly rather than providing normal, orderly form cleanup. It can leave the project in a confusing state and interrupt other running code. Unload the intended form instead; if the goal is to stop a particular procedure, use a deliberate control-flow approach such as exiting that procedure.
Quitting Excel is a separate action
If the real intention is to close the workbook or exit Excel, handle that as a separate operation and consider unsaved work. Unloading a UserForm has no effect on the workbook window. Closing a workbook can prompt to save changes, while Application.Quit asks Excel itself to exit and may affect other open workbooks. A UserForm’s ordinary Cancel or Quit button should generally close only the form, unless the interface clearly tells the user that the entire application is about to close.
Frequently Asked Questions
What is the VBA code to close a UserForm?
In code behind the form, use Unload Me to close and unload the current form. Use Me.Hide if you need to keep its instance and control values available.
Is there a UserForm.Close command?
No. VBA’s standard approach is to hide a form with Hide or unload it with Unload. A Close method is not the normal UserForm syntax.
Does Unload Me close Excel?
No. It unloads the form whose code is running. It does not close the workbook or quit Excel.
How do I stop users closing a form with the X?
Use the form’s QueryClose event and set its Cancel argument to True when CloseMode is vbFormControlMenu. Provide another clear way to exit if you block the X.
Why are my UserForm values still there after I close it?
If the form was hidden, it remains loaded and retains its control values. Unload it when those values are no longer needed, or reset the controls before showing the form again.
Related
- Excel Data Validation Source: Lists, Ranges, and Rules
- Excel Data Validation Rules: How They Work and When to Use Them
- Excel Data Validation Restrictions and Their Limits
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Function Examples: Useful Worksheet UDFsExcel VBA function examples show how to create custom worksheet functions for tasks that built-in formulas do not…
- VBA Create New Workbook: Code, Options, and Tips in ExcelTo create a new Excel workbook with VBA, use Workbooks.Add. For example, Set newWb = Workbooks.Add creates a…
- Excel VBA Active Pivot Table: How to Find and ControlIn Excel VBA, get the PivotTable associated with the active cell by using ActiveCell.PivotTable; Excel does not provide…
- VBA Set Statement: How to Assign Object ReferencesIn VBA, Set is a statement—not a function—that assigns an object reference to an object variable. Use it…
- VBA Functions: Basics, Examples, and How to Write ThemA VBA function is a named block of code that performs a task and returns a value. In…
- Can You Make Macros in Google Sheets? How They WorkYes. You can make macros in Google Sheets by recording a sequence of actions in the browser; Sheets…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.