What is Worksheet Events in excel VBA?

These are the types of events that would trigger based on the actions taken in the worksheet.

Examples of worksheet events

  1. Changing a cell in the worksheet
  2. Changing the selection
  3. Double-clicking on a cell
  4. Right-clicking on a cell
Event NameWhat triggers the event
ActivateWhen the worksheet is activated
BeforeDeleteBefore the worksheet is deleted
BeforeDoubleClickBefore the worksheet is double-clicked
BeforeRightClickBefore the worksheet is right-clicked
CalculateBefore the worksheet is calculated or recalculated
ChangeWhen the cells in the worksheet are changed
DeactivateWhen the worksheet is deactivated
PivotTableUpdateWhen the Pivot Table in the worksheet is updated
SelectionChangeWhen the selection on the worksheet is changed

1) Worksheet Activate event:-

Occurs when a workbook, worksheet, chart sheet, or embedded chart is activated.

Examples 1

The below code unprotects a sheet as soon as it is activated.

Private Sub Worksheet_Activate()
End Sub

Example 2

We can use this event to make sure a specific cell or a range of cells (or a named range) is selected as soon as you activate the worksheet. The below code would select cell K1 as soon as you activate the sheet.

Private Sub Worksheet_Activate()
End Sub

2) Worksheet Selection change events:-

The selection change event is triggered whenever there is a selection change in the worksheet.

This example of this event is when you want to highlight the active row and column of the selected cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = xlNone
With ActiveCell
.EntireRow.Interior.Color = RGB(20, 123, 73)
.EntireColumn.Interior.Color = RGB(177, 98, 125)
End With
End Sub

The code first removes the background color from all the cells and then apply the one mentioned in the code to the active row and column.