Prevent a userfrom from closing when the user clicks the x-button by using VBA
Tags: preventauserfromfrom, vbamacros
Published: 2021-10-13 02:16:46

---
title: Prevent a userfrom from closing when the user clicks the x-button by using VBA
published: true
date: 2021-10-12 10:58:57 UTC
tags: PreventAUserfromFrom,VBAMacros
canonical_url: https://geekexcel.com/prevent-a-userfrom-from-closing-when-the-user-clicks-the-x-button-by-using-vba/
---

In this tutorial, you will learn how to  **prevent an userfrom from closing when the user clicks the X-Button by**   **using VBA ** in Microsoft Excel. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## Example

- Firstly, you need to **create an userform** which can be activated by clicking the **run button** on the worksheet.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Sample-data-23.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Option Explicit
Sub running()
UserForm1.Show
End Sub

'Add below code in userform
Private Sub CommandButton1_Click()
'Close the userform
Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Using Query Close event of Userform
'Comparing the constant value of CloseMode variable
'If it is equal to inbuilt constant of control menu
'Then prevent closing of userform and
'Display information message dialog box
If CloseMode = vbFormControlMenu Then  
    'Changing Cancel variable value to True
    'By default, it is False
    Cancel = True
    MsgBox "You can't close the dialog like this!"
End If
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Save-the-Code-38.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Run-the-Code-30.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive the output as an **information message** in the dialog box in Excel.

## Sum-Up

In the above short tutorial, we have illustrated ** ** how to  **prevent an userfrom from closing when the user clicks the X-Button by using VBA**  in Microsoft Excel. Hope you like this article. Please feel free to state your  **query**  or  **feedback**  for the above article. Thank you so much for Reading!! To learn more, check out **[Geek Excel](https://geekexcel.com/)!! **And [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[How to Create a Gantt Chart with Several Clicks in Excel?](https://geekexcel.com/how-to-create-a-gantt-chart-with-several-clicks-in-excel/)**
- **[How to Prevent From Changing Worksheet Name in Microsoft Excel?](https://geekexcel.com/how-to-prevent-from-changing-worksheet-name-in-microsoft-excel/)**
- **[Formulas to Get the Percent Change ~ Quick Method!!](https://geekexcel.com/excel-formulas-to-get-the-percent-change/)**
- **[Excel Formulas to Check Date is Same Month ~ A Complete Tutorial!!](https://geekexcel.com/excel-formulas-to-check-date-is-same-month/)**
- **[Excel Formulas to Get the First Word of a Text ~ Useful Guide!!](https://geekexcel.com/excel-formulas-to-get-the-first-word-of-a-text/)**