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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
A common part of working with text strings in a worksheet is normalizing those strings so that they follow whatever rules ...
Discover MoreWhen working with data in a macro, there are two broad categories you can manipulate: numbers and text. Sometimes you ...
Discover MoreWorkbooks get corrupted from time to time; that's a fact of life in an Excel world. If those corrupted workbooks contain ...
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