How To Create event macros for the Application object using VBA in Excel?
Tags: tocreateeventmacrosf, vbamacros
Published: 2021-10-11 04:13:38

---
title: How To Create event macros for the Application object using VBA in Excel?
published: true
date: 2021-10-08 09:08:33 UTC
tags: ToCreateEventMacrosF,VBAMacros
canonical_url: https://geekexcel.com/how-to-create-event-macros-for-the-application-object-using-vba-in-excel/
---

In this tutorial, you will learn how to  **create event macros for the application object 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, 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://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to click on the **Insert** and select **Module**.

![Insert a module](https://geekexcel.com/wp-content/uploads/2021/09/Insert-a-module.png)<figcaption>Insert a module</figcaption>

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

```
Public WithEvents Appl As Application

Private Sub Appl_NewWorkbook(ByVal Wb As Workbook)
    ' your code here
    MsgBox "A new workbook is created!"
End Sub

Private Sub Appl_WorkbookBeforeClose(ByVal Wb As Workbook, _
    Cancel As Boolean)
    ' your code here
    MsgBox "A workbook is closed!"
End Sub

Private Sub Appl_WorkbookBeforePrint(ByVal Wb As Workbook, _
    Cancel As Boolean)
    ' your code here
    MsgBox "A workbook is printed!"
End Sub

Private Sub Appl_WorkbookBeforeSave(ByVal Wb As Workbook, _
    ByVal SaveAsUI As Boolean, Cancel As Boolean)
    ' your code here
    MsgBox "A workbook is saved!"
End Sub

Private Sub Appl_WorkbookOpen(ByVal Wb As Workbook)
    ' your code here
    MsgBox "A workbook is opened!"
End Sub

```

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

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Code-27.png)<figcaption>Save the Code</figcaption>

- Then, after you have **finished editing the event macros** for the **Application object** , you have to add some code to the module **ThisWorkbook to activate the new event macros**.

```
Dim ApplicationClass As New AppEventClass

Private Sub Workbook_Open()
    Set ApplicationClass.Appl = Application
End Sub

```

- Now, you have to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Another-Code.png)<figcaption>Another Code</figcaption>

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

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

- Finally, after you **run the Workbook\_Open procedure** , the **events** attached to the **Application object are activated**.

## Wrap-Up

We hope that this tutorial gives you guidelines on how to  **create event macros for the application object using VBA**  in Microsoft Excel. ** ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Also:**

- **[How to Create Application Level Events in Excel VBA?](https://geekexcel.com/how-to-create-application-level-events-in-excel-vba/)**
- **[How to Change Event To Run Macro When any Change is Made using Worksheet?](https://geekexcel.com/how-to-change-event-to-run-macro-when-any-change-is-made-using-worksheet/)**
- **[How To Find method of Range object in Excel VBA?](https://geekexcel.com/how-to-find-method-of-range-object-in-excel-vba/)**
- **[What is CreateObject Method in VBA & How to use CreateObject Method in Excel?](https://geekexcel.com/what-is-createobject-method-in-vba-how-to-use-createobject-method-in-excel/)**