Written by Allen Wyatt (last updated June 1, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365
John needs to ensure that certain actions have taken place (information added, etc.) before a user leaves a worksheet. He wonders if there is some sort of macro event such as WorksheetBeforeDeactivate.
There are actually two events you could use for this purpose. You can use the SheetDeactivate event in the ThisWorkbook module to trigger actions whenever a user leaves any worksheet in the workbook:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object) MsgBox Prompt:="You just left sheet:" & Sh.Name End Sub
If you want to trigger actions only when they leave a particular worksheet, then you can use the Deactivate event in the WorkSheet object:
Private Sub Worksheet_Deactivate() ' sheet specific code goes here End Sub
You should know, however, that in either case the worksheet to which the user is choosing to go will be the active worksheet after the event is completed. If you want to force the user to stay on the worksheet, you need to specifically put them back on the worksheet, in this manner:
Private Sub Worksheet_Deactivate() ' sheet specific code goes here Sheets("Sheet1").Select End Sub
This assumes, of course, that the name of the worksheet you want the user to remain on is Sheet1.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7730) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Triggering an Event when a Worksheet is Deactivated.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
With a little bit of work, Excel allows you to format individual characters of the text you place in a cell. If you want ...
Discover MoreAs you format your worksheet, Excel allows you to add page breaks where you'd like. If you want to put in a series of ...
Discover MoreDo you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-06-03 12:48:49
J. Woolley
To simply re-activate a worksheet, the Worksheet.Activate method is preferred:
Private Sub Worksheet_Deactivate()
' sheet specific code goes here
If MsgBox("Re-activate " & Me.Name & "?", vbYesNo) = vbYes Then
Me.Activate
End If
End Sub
The Worksheet.Select method is normally used to group/ungroup multiple sheets.
2024-06-01 08:16:54
Alex Blakenburg
Rather than hard code the Sheet name you could use
Me.Select
Not a great idea though since it will make it impossible to go to a different sheet.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments