Used in class modules to define a variable that can receive events.
Can only be used in class modules.
Can only be declared at the module level.
Worksheet Level Event Handling
There are several Excel objects that have events automatically built-in.
Worksheet Level events are the easiest to see and also the simpliest to use.
Understanding how Worksheet Level Events work is essential before trying to create your own user defined events.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application Level Event Handling
Most of the time worksheet level or workbook level events can provide you with everything you need.
When you need to replicate functionality across multiple workbooks you have two options.
Option 1 - Copy and paste the VBA code into every workbook (this will be a nightmare to maintain, and is not recommended).
Option 2 - Put all your VBA code into Excel add-in which is available regardless of which workbook is open. This option would use built-in Application Level events.
Although the Application Level events are built-in, you need to write your own event handler routine to capture them.
Understanding how Application Level Events work is essential before trying to create your own user defined events.
Public WithEvents ExcelAppEvents As Application
User Defined Event Handling
Also known as Class Module Level Events.
This allows you to create objects that can expose user defined events.
The WithEvents keyword can only be used in class modules.
Public WithEvents myEvents As ClassWithEvents
The Event keyword is used to declare a user defined event.
Public Event MyTextChangedEvent(ByVal myString As String)
The RaiseEvent keyword is used to trigger the event.
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext