Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Triggering an Event when a Worksheet is Deactivated.
Written by Allen Wyatt (last updated June 1, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
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, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Triggering an Event when a Worksheet is Deactivated.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
When you record a macro, make sure that you know how Excel is recording your cell movements. This tip explains the ...
Discover MoreIf you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...
Discover MoreWant to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how to ...
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 © 2024 Sharon Parq Associates, Inc.
Comments