WithEvents

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 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) 
End Sub

Application 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.
The first option is to copy and paste the VBA code into every workbook although this will be a nightmare to maintain.
The alternative is to to keep all your VBA code in one place by using an Excel add-in which is available regardless of which workbook is currently open.
In this scenario you would need to use the 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 

Class Module Event Handling


Public WithEvents myEvents As ClassWithEvents 



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext