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.

Triggering an Event when a Worksheet is Deactivated

Written by Allen Wyatt (last updated November 10, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003


2

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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Keeping Full Menus Displayed

Word allows menus to be displayed in two modes. The default mode, which displays menu options dynamically, drives some ...

Discover More

Calculating Only the Active Workbook

When you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on ...

Discover More

Strange Tab Settings in Bulleted Items

Getting bulleted lists to look just right can be challenging, particularly if you are using the Bullet tool on the Home ...

Discover More

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!

More ExcelTips (menu)

Telling which Worksheets are Selected

If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out ...

Discover More

Error Using ATAN2 Function in Macro

Excel allows you to use worksheet functions from within macros. This is helpful, especially when you are trying to ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2019-03-23 12:27:09

Willy Vanhaelen

@Tobias
The macro does excactly what the code tells it to do:
- ignore the line starting with an apostrophe: 'sheet specific code goes here
- select sheet1
- End Sub = stop

If you want the macro to do something you must replace the first line with the code you want it to execute when Sheet2 is deactivated.
For example : MsgBox "you just left Sheet2"
Be aware that, being in Sheet2, you select another sheet, that sheet is first activated and then your code in Sheet2 is executed.

If you only have 2 sheets, as in your example, the last line: Sheets("Sheet1").Select has no sence since you can only leave Sheet2 by selecting Sheet1. So the macro doesn't have to select Sheet1: you already did -:)


2019-03-22 07:42:48

Tobias

The code above, namely;

Private Sub Worksheet_Deactivate()
' sheet specific code goes here
Sheets("Sheet1").Select
End Sub

does not work as advertised when tested using Excel 2010 - in a new workbook with 2 sheets; happy to send the example I created.

In the example when you select Sheet1 (coming from Sheet2), the code fires but Sheet1 is activated and it stops there ?

Good resource by the way, Thanks


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.