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 one 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
In Excel VBA it's fairly easy to create events which work on a single worksheet or workbook. Let's say we want to use an event procedure to change the width of a column as we enter data into cells:
We want the column to change its width automatically as we enter data.
We can make this work on a single worksheet quite easily. In the Visual Basic Editor, double-click the worksheet whose events you want to access:
Double-click the worksheet in the Project Explorer window.
Use the drop down lists at the top of the code window to select the relevant object and event:
Select Worksheet from the drop down list on the left, and Change from the list on the right.
When you select Worksheet from the drop down list on the left, VBA will insert the default worksheet event called Worksheet_SelectionChange. After you have selected the Change event, you can delete the default event.
Add code to the event procedure to perform whatever actions you want. The example code shown below will change the width of the column to match its widest entry:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.EntireColumn.AutoFit
End Sub
This code will now run automatically each time you change the value of a cell on the selected worksheet.
If you want the same event to work on every worksheet in the workbook, you can instead create a workbook level event procedure. To do this, start by double-clicking the ThisWorkbook object in the Visual Basic Editor:
Double-click ThisWorkbook in the Project Explorer window.
Use the drop down lists at the top of the code window to select the Workbook object and the SheetChange event:
Choose Workbook from the left-hand drop list and SheetChange from the right-hand drop list.
Add the same code to the new event, as shown below:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.EntireColumn.AutoFit
End Sub
This code will run automatically when you change the value of a cell on any worksheet in the workbook.
The only problem with the example shown above is that it will only work in the workbook that you've added the code to. The next part of this blog shows you how to make it work for any workbook!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
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.