» Create eventmacros for the Application object using VBA in Microsoft Excel
VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
CATEGORY - Events in VBA
VERSION - All Microsoft Excel Versions
Start the Visual Basic editor.Select the desired project in the Project-window.
Insert a new class module by selecting the menu Insert | Class Module.
Activate the new class module and rename it, e.g. AppEventClass
Copy and paste these example macros to the new class module:
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
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
After you run the Workbook_Open procedure, the events attached to the Application object are activated.
Book Store:
Recommended Books:
- VBA for Modelers: Developing Decision Support Systems Using Microsoft« Excel
- Mastering Excel 2000 (for beginner)
- Microsoft Office Xp: Advanced Concepts and Techniques: Word 2002, Excel 2002, Access 2002, Powerpoint 2002
- Managerial Accounting: Tools for Business Decision Making, WebCT, 2nd Edition
- The McGraw-Hill Guide to Writing a High-Impact Business Plan: A Proven Blueprint for First-Time Entrepreneurs
- Mortgages For Dummies®
No comments have been submitted.

