How to Run a Macro Automatically Before Workbook Close in Excel?
Tags: torunamacroautomatic, vbamacros
Published: 2021-09-04 01:54:57

---
title: How to Run a Macro Automatically Before Workbook Close in Excel?
published: true
date: 2021-09-03 14:25:45 UTC
tags: ToRunAMacroAutomatic,VBAMacros
canonical_url: https://geekexcel.com/how-to-run-a-macro-automatically-before-workbook-close-in-excel/
---

In this article, you will learn **how to run a Macro automatically before a Workbook closes in Excel Office 365**. 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)

## Auto Run Macro When Workbook Closes

- To **run macro as the workbook closes** , you have to use **Workbook Event Workbook\_BeforeClose**.
- The **syntax** of this event is given below.

```
Private Sub Workbook_BeforeClose(Cancel As Boolean)

 'Your Code here.

End Sub

```

- Firstly, 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.

![](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-4-1024x180.png)<figcaption>Select Visual Basics</figcaption>

- Now, in **project explorer** , you have to find **ThisWorkbook object** in project explorer and **double-click** on it.

![](https://geekexcel.com/wp-content/uploads/2021/09/find-ThisWorkbook-object.png)<figcaption>find ThisWorkbook object</figcaption>

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

```
 Private Sub Workbook_BeforeClose(Cancel As Boolean)
 ThisWorkbook.Save
MsgBox "This Workbook is saved."

End Sub

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Save-the-code-5.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.

![](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-4-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to **close the Excel Worksheet**.
- Finally, your **workbook will be automatically saved** , and the **message** will be shown as given below.

![](https://geekexcel.com/wp-content/uploads/2021/09/Workbook-Saved-1024x659.png)<figcaption>Workbook Saved</figcaption>

## Verdict

From this chapter, you can get some clarification about **how to run a Macro automatically before a Workbook closes in Excel Office 365**. Let me know if you have any **doubts** regarding this article or any other Excel/VBA topic.

Thank you so much for visiting  **[Geek Excel](https://geekexcel.com/)!! **If you want to learn more helpful formulas, check out [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Ahead:**

- **[How to Create and Insert Auto Text Entry in Excel Office 365?](https://geekexcel.com/how-to-create-and-insert-auto-text-entry-in-excel-office-365/)**
- **[Excel VBA Loops Statements in Office 365 ~ With Examples!!](https://geekexcel.com/excel-vba-loops-statements-in-office-365-with-examples/)**
- **[Excel VBA Decisions in Office 365 ~ Easy Tutorial for You!!](https://geekexcel.com/excel-vba-decisions-in-office-365-easy-tutorial-for-you/)**
- **[Auto Resize Comment Boxes to Fit the Contents in Excel Office 365!!](https://geekexcel.com/auto-resize-comment-boxes-to-fit-the-contents-in-excel-office-365/)**
- **[Excel Formulas to Find the Next Scheduled Event Date from Today!!](https://geekexcel.com/excel-formulas-to-find-the-next-scheduled-event-date-from-today/)**