Worksheet Events
Events are occurrences that
objects can respond to. Event macros are triggered by an event
such as opening a double clicking a cell, right click, opening a workbook, or a
activating a worksheet
Worksheet_Change Event
triggers when you change a cell (or range of cells) value manually or in a
macro.
It will not be triggered from a change showing up in a formula or
from a change of format. Your change macro might be used to automatically
provide a date to another cell.
Unlike standard
macros, the worksheet event macros are installed directly from the worksheet.
Here are some Worksheet Events available in Excel.
Private Sub Worksheet_Activate()
Private Sub Worksheet_BeforeDoubleClick (ByVal Target As Range, Cancel As
Boolean)
Cancel = True 'turn off Edit mode
when using “Edit directly in a cell”
Private Sub Worksheet_BeforeRightClick (ByVal Target As Range, Cancel As
Boolean)
Cancel = True 'turn off Edit mode
when using “Edit directly in a cell”
Private Sub Worksheet_Calculate()
Private Sub Worksheet_Change (ByVal Target As Range)
Application.EnableEvents = False
'should be part of Change macro
Application.EnableEvents = True
'should be part of Change macro
Private Sub Worksheet_Deactivate ()
Private Sub Worksheet_FollowHyperlink (ByVal Target As Hyperlink)
Private Sub Worksheet_SelectionChange (ByVal Target As Range)
Workbook Events
Unlike standard macros which are installed in
standard modules, Workbook Events are installed in ThisWorkBook (Ctrl+R or under
menu View -> Project Explorer).
Here are some examples.
Private Sub Workbook_AddinUninstall()
Private Sub Workbook_BeforeClose (Cancel As Boolean)
Private Sub Workbook_BeforePrint (Cancel As Boolean)
Private Sub Workbook_BeforeSave (ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Private Sub Workbook_Deactivate()
Private Sub Workbook_NewSheet (ByVal Sh As Object)
Private Sub Workbook_Open()
Private Sub Workbook_SheetActivate (ByVal Sh As Object)
Private Sub Workbook_SheetBeforeDoubleClick (ByVal Sh As Object, ByVal
Target As Range, Cancel As Boolean)
Private Sub Workbook_SheetBeforeRightClick (ByVal Sh As Object, ByVal
Target As Range, Cancel As Boolean)
Private Sub Workbook_SheetCalculate (ByVal Sh As Object)
Private Sub Workbook_SheetChange (ByVal Sh As Object, ByVal Target As
Range)
Private Sub Workbook_SheetDeactivate (ByVal Sh As Object)
Private Sub Workbook_SheetFollowHyperlink (ByVal Sh As Object, ByVal
Target As Hyperlink)
Private Sub Workbook_SheetSelectionChange (ByVal Sh As Object, ByVal
Target As Range)
Private Sub Workbook_WindowActivate (ByVal Wn As Window)
Private Sub Workbook_WindowDeactivate (ByVal Wn As Window)
Private Sub Workbook_WindowResize (ByVal Wn As Window)