Excel VBA UserForm Close X Button: How to Disable or Control
In this article
To control or disable the 'X' close button on an Excel VBA UserForm, you must use the QueryClose event within the UserForm's code module. Because VBA does not provide a simple property in the Properties window to hide the 'X' button, you must write a small piece of code that intercepts the close command and cancels it if the user clicks the top-right corner. This ensures that users interact with your form via the buttons you have provided (such as 'Submit' or 'Cancel'), preventing them from accidentally closing the form and leaving data in an incomplete state.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- A workbook saved as an Excel Macro-Enabled Workbook (.xlsm).
- The Visual Basic for Applications (VBA) editor open.
- An existing UserForm created within your VBA project.
Step-by-Step
- Open the VBA Editor: Press
Alt + F11on your keyboard while in Excel to open the Visual Basic Editor. - Locate Your UserForm: In the Project Explorer window on the left side, find the folder labeled
Forms. Double-click the specific UserForm you wish to modify to open its visual designer. - Open the Code Window: Right-click anywhere on the UserForm background and select
View Code. This opens the code module associated specifically with that form. - Create the QueryClose Event: In the code window, you will see two dropdown menus at the top. In the left dropdown (which lists objects), select
UserForm. In the right dropdown (which lists events), selectQueryClose. VBA will automatically generate the following subroutine structure:Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) End Sub
- Identify the CloseMode Value: The
CloseModeargument tells VBA how the form is being closed. When a user clicks the 'X' button in the top-right corner, theCloseModevalue is0(vbFormControlStatusButtton). - Add the Cancellation Logic: To stop the form from closing when the 'X' is clicked, you need to set the
Cancelargument to1(which represents True). Enter the following code inside the subroutine:If CloseMode = 0 Then Cancel = 1 MsgBox "Please use the provided buttons to close this form.", vbInformation, "Invalid Action" End If - Test the Implementation: Press
F5to run the UserForm. Try clicking the 'X' button in the top-right corner. You should see your message box appear, and the form should remain open. - Create a Valid Exit Path: Since the 'X' button is now disabled, you must provide a way for the user to close the form. Add a CommandButton to your form (e.g., named
btnCancel) and add the following code to its click event:Private Sub btnCancel_Click() Unload Me End Sub
Common Mistakes
One of the most frequent errors is attempting to find a "CloseButton = False" property in the UserForm Properties window. Many beginners spend hours searching for a checkbox to disable the 'X' button, but this property does not exist in standard VBA. The only way to achieve this without using complex Windows API calls is through the QueryClose event.
Another common mistake is forgetting to provide an alternative exit button. If you disable the 'X' button and your 'Cancel' or 'Close' button has a bug or is missing, the user becomes trapped in the UserForm. In some cases, the only way out is to force-close Excel via the Task Manager, which can lead to loss of unsaved data. Always test your exit buttons before deploying the tool to other users.
Some developers mistakenly use the UserForm_Terminate event instead of UserForm_QueryClose. The Terminate event occurs after the form has already begun the closing process; it is used for cleaning up memory or closing database connections. It cannot stop the form from closing. Only QueryClose has the Cancel parameter necessary to abort the closing action.
Tips
To make the user experience smoother, customize the MsgBox text to explain why the 'X' button is disabled. Instead of a generic "Invalid Action," use phrasing like "Please complete the required fields before exiting" or "To ensure your data is saved, please click the Submit button." This reduces user frustration.
If you have multiple UserForms in your project, you can create a standard module with a public procedure to handle the closing logic. However, because QueryClose is an event specific to the form instance, you will still need to call that procedure from within each form's QueryClose event.
For a more professional look, consider using the Unload Me command rather than End. The End keyword stops all VBA execution immediately and clears all global variables, which can cause unexpected behavior in other parts of your application. Unload Me specifically removes the current form from memory while allowing other macros to continue running.
Troubleshooting
If the 'X' button still closes the form after you have added the code, first check the CloseMode value. Ensure you are checking for CloseMode = 0. If you check for CloseMode = 1, you are actually blocking the Unload command, which will prevent your own 'Cancel' buttons from working.
Check for syntax errors in the VBA editor. If the code is highlighted in red, the macro will not run. Ensure that the Cancel = 1 line is placed inside the If...End If block. If it is placed outside the block, the form will never close, regardless of how it is triggered.
If the UserForm is flickering or not responding to the click, ensure that you do not have conflicting events (like UserForm_Layout or UserForm_Initialize) that are triggering infinite loops. Restart the VBA editor and run the form again to clear any cached runtime errors.
If you are using a 64-bit version of Excel and attempting to use advanced API calls to truly hide the button (rather than just blocking the click), ensure you have included the PtrSafe attribute in your declarations. However, for most users, the QueryClose method described above is the safest and most compatible approach across all Excel versions.
Frequently Asked Questions
Can I completely hide the X button so it is invisible?
Not through standard VBA properties. To make the button physically disappear, you would need to use Windows API calls to modify the window style of the UserForm. This is significantly more complex and can cause stability issues across different versions of Windows.
Will this prevent the form from closing if I use Unload Me in another macro?
No. The CloseMode for Unload Me is 1. Since the code only checks for CloseMode = 0 (the 'X' button), your programmatic commands to close the form will still work perfectly.
Does this work on Mac versions of Excel?
VBA behavior on macOS differs significantly from Windows. The QueryClose event may not behave the same way, and the CloseMode constants may not apply. This method is specifically designed for the Windows environment.
What happens if the user presses Alt+F4?
Pressing Alt+F4 triggers the same event as clicking the 'X' button. Therefore, the QueryClose code will intercept Alt+F4 and block it just as it blocks the mouse click.
Can I ask the user for confirmation before closing?
Yes. Instead of simply setting Cancel = 1, you can use a MsgBox with vbYesNo. If the user selects "No," you set Cancel = 1 to keep the form open; if they select "Yes," you leave Cancel as 0 to allow the form to close.