Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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: Running a Macro When a Worksheet is Deactivated.

Running a Macro when a Worksheet is Deactivated

Written by Allen Wyatt (last updated November 9, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


It is possible to configure Excel so that a macro of your choosing is executed every time a particular worksheet is deactivated. What does that mean? Simply that a macro can be run every time you click on a worksheet tab to leave the current sheet. There are two ways you can do this. Here's the first method:

  1. Activate the worksheet with which you want the macro associated.
  2. Make sure the Formulas tab of the ribbon is displayed.
  3. In the Defined Names area of the ribbon, click Define Name. Excel displays the New Name dialog box. (See Figure 1.)
  4. Figure 1. The New Name dialog box.

  5. In the Name field, enter a name that begins with the worksheet name, followed by an exclamation point, Auto_Deactivate, and any other wording desired. Thus, if the worksheet were named Stocks, you might enter Stocks!Auto_Deactivate_Exit.
  6. In the Refers to box, enter a formula that points to the workbook and macro you want automatically executed. Thus, if the macro name were Update_PL, and the workbook name were PFOLIO.XLS, you would enter the formula =PFolio!Update_PL.
  7. Click on the OK button.

The second method is to rely on the Worksheet_Deactivate event. Right-click the worksheet's tab (at the bottom of the Excel window) and choose Code Window from the resulting options. This opens the Visual Basic Editor and you can then put the following into the Code window:

Private Sub Worksheet_Deactivate()
    Call Update_PL
End Sub

This, again, assumes that the macro you want to run is called Update_PL. You can then close the Visual Basic Editor.

Remember that the macro you set up using either of these approaches is run every time the worksheet is deactivated, not just the first time. Think about how you use Excel; if you spend a fair amount of time hopping between worksheets in a workbook or between workbooks, it is possible to deactivate a worksheet several dozen times during the course of a session.

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 (6138) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Running a Macro 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

Determining the Number of Visible Columns

When using a macro to process information in a worksheet, you may want that macro to figure out how many columns are ...

Discover More

Quickly Entering Data

Excel includes a handy shortcut for entering data that is similar to whatever you entered in the cell above your entry ...

Discover More

Sending Drawing Objects to the Back or Front

Drawing objects can be placed in a document in such a way that they overlap with each other. If you want to arrange those ...

Discover More

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!

More ExcelTips (ribbon)

Selecting the First Cell In a Row

When creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...

Discover More

Using Seek In a Macro

When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...

Discover More

Retrieving Drive Statistics

Need to gather some information about the drives on a system? It can be pretty easy to do using a macro, as shown in this ...

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 seven more than 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

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.