Worksheet Events

Worksheet Events

Activate
Use the “Activate” event when you want some code to run before getting in to the sheet being activated.
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]

Private Sub Worksheet_Activate()
‘code here
End Sub
[/sourcecode]

BeforeDoubleClick
Use this to run code before a double-click
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]

Private Sub Worksheet_BeforeDoubleClick()
‘code here
End Sub
[/sourcecode]

BeforeRightClick
Use this to run code before a right-click
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
‘This code will hide the sheet when you click on another sheet
Private Sub Worksheet_BeforeRightClick()
‘code here
End Sub
[/sourcecode]

Calculate
Use this to run code when the sheet calculates
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]

Private Sub Worksheet_Calculate()
‘code here
End Sub
[/sourcecode]

Change
Use this to run code when the sheet changes in some way
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]

Private Sub Worksheet_Change()
‘code here
End Sub
[/sourcecode]

FollowHyperlink
Use this to run code when a hyperlink on the sheet is selected

Private Sub Worksheet_FollowHyperlink()
‘code here
End Sub

PivotTableUpdate
Use this to run code when a PivotTable is updated
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]

Private Sub Worksheet_PivotTableUpdate()
‘code here
End Sub
[/sourcecode]

SelectionChange
Use this to run code when the selection changes in some way
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]

Private Sub Worksheet_SelectionChange()
‘code here
End Sub
[/sourcecode]

Deactivate
Use this event to run code as you leave the sheet.
[sourcecode language=”vb” light=”true” wraplines=”false” gutter=”false”]
‘This code will hide the sheet when you click on another sheet
Private Sub Worksheet_Deactivate()
Sheets("Sheet1").Visible = False
End Sub
[/sourcecode]