Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Triggering an Event when a Worksheet is Deactivated.
by Allen Wyatt
(last updated November 10, 2018)
John needs to ensure that certain actions have taken place (information added, etc.) before a user leaves a worksheet. He wonders if there is there 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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7728) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Triggering an Event when a Worksheet is Deactivated.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
When developing a macro, it is often necessary to step through the various code lines so you can see what is happening on ...Discover More
When processing information in a macro, you often need to select different cells relative to the currently selected ...Discover More
Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros, ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.