560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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
Event-handling macros in Excel Visual Basic
Part five of a five-part series of blogs
Want to do something when a user opens a workbook, or stop them clicking on a particular cell? You need to learn how to attach code to Excel workbook or worksheet events!
Although this blog is mainly about Excel VBA, I thought I'd briefly show how events work in various other Microsoft Office applications. I've used Office 2010 for all of the examples on this page, but the ideas would work just as well in Office 2007.
Microsoft Word follows pretty much the same pattern as Microsoft Excel:
You can double-click on ThisDocument to attach code to an event for the document.
The list of events available isn't as rich as it is for Excel:
The list of events doesn't include BeforeClose, for example.
PowerPoint doesn't allow you to attach code to events easily:
There is a presentation open, but you can't see that in Project Explorer.
However, as for Word there is a way to attach code to events for a presentation by creating something called an event sink (see above).
Even if you do create a PowerPoint event sink, you still won't have truly solved the problem, as you still need something to run a macro to create the event sink in the first place!
The whole of Access form and report design is based around the concept of events:
This diagram shows the events available for a simple label control (a bit of text stuck on a form).
Whereas events in Excel, Word and PowerPoint are optional extras, you won't be able to write VBA code in Access forms or reports without using them all the time.
The events which can occur in an Outlook session are shown below:
Want to create a rule for how new mail should be treated? Don't bother with the Outlook rules - why not create your own in VBA!
Outlook macros are all held in a single file with an .OTM extension.
As the above examples show, handling events in Excel VBA is - as is so often the case - easier than doing the same thing in other MS Office applications.
|Parts of this blog|
25 Aytoun Street