Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Application level events in Excel VBA Part three of a three-part series of blogs |
---|
In Excel VBA it's pretty easy to create event procedures which apply to a single worksheet or workbook. But what if you want an event to work in every workbook you open? This blog explains how to create application level event procedures in Excel VBA. You'll learn how to create a class module, how to declare application variables which support events and how to enable and disable application level events.
|
In this blog
One easy way to get code to run automatically whenever you open Excel is to use the Workbook_Open event of the Personal Macro Workbook. The Personal Macro Workbook is a hidden workbook stored as part of your user profile.
When you've created the Personal Macro Workbook, you'll find it in the XLSTART folder.
Whenever you open Excel it checks if the Personal Macro Workbook exists and, if so, opens it automatically as a hidden workbook. This means that any code you store in it will be available whenever you open Excel.
Perhaps the easiest way to start creating the Personal Macro Workbook is to choose to record a macro. You can see one way to begin recording a macro in the image below:
Click this button in the bottom left corner of the Excel window to begin recording a macro.
On the dialog box which appears, choose to store the macro in the Personal Macro Workbook.
Choose the option shown to store the macro in the Personal Macro Workbook.
When you click OK the macro begins recording. Your macro doesn't need to do anything, so you can immediately stop recording as shown in the image below:
Click the square in the bottom left corner of the Excel window to stop recording.
When you return to the Visual Basic Editor you should see the Personal Macro Workbook in the Project Explorer window.
The PERSONAL.XLSB workbook is visible in the Visual Basic Editor but not in Excel.
You can delete the module from the Personal Macro Workbook as you won't need it. You can do this as shown in the image below:
Right click on the module and choose to remove it. Click No on the dialog box which appears.
Now you can copy the class module and normal module from the other workbook into the Personal Macro Workbook. The easiest way to do this is simply to click and drag the modules from one project to the other in the Project Explorer window as shown below:
Click and drag a module from one project to another.
Now you can add code to the Workbook_Open event of the Personal Macro Workbook. You can do this in the same way as for any other workbook. Start by double-clicking the ThisWorkbook object.
Double-click the ThisWorkbook object to open its code window.
Select the Workbook object from the drop down list at the top left of the code window.
Choosing Workbook adds the Open event automatically.
In the Workbook_Open event, add an instruction to call the Activate_App_Level_Events subroutine from Module1.
Private Sub Workbook_Open()
Activate_App_Level_Events
End Sub
The code shown above runs automatically each time you open Excel.
To save the Personal Macro Workbook, select any item which belongs to it in the Project Explorer window and then click the Save button as shown below:
Click any item in the PERSONAL.XLSB project then click the button shown to save it.
You can now close Excel without saving any changes to the other open workbook.
To test that your code works, all you need to do is open a fresh copy of Excel and create a new workbook. You should find that your events work automatically without any further effort from you!
The final result of your hard work!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
From: | James Martin |
When: | 16 Oct 24 at 13:40 |
This is a brilliant blog. Been looking for a workable App Event Level Event Trigger for a while. The code on other sites was not working. Looking to automatically trigger a few different events including A "Macro L" (saved on LinkedIn) I have to log all active window changes I have on a performance monitoring Sheet. On the Freeze Pane it records performance stats including Lock Screen breaks. It also records Macro uses, and I have been getting 2.5 hours of time savings with 2,500 Active Windows in a standard shift day, for a previous repetitive role.
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.