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

Protecting Tracked Changes

Track Changes is a great tool for editors and collaborators to use when creating documents. An author, seeking changes ...

Discover More

Easy Filtering Specifications for a PivotTable

When you want to include specific records from a source table into a PivotTable, you need to employ some sort of ...

Discover More

Automatically Renaming Worksheets

Excel allows you to easily add and remove worksheets from a workbook. You may want a way to automatically rename all of ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Putting the Last Saved Date in a Cell

Do you need a cell in your worksheet to display the date on which the workbook was last saved? This can be a bit tricky, ...

Discover More

Generating a List of Macros

Got a workbook that has lots and lots of macros associated with it? Here's a way you can get a list of all of those ...

Discover More

Copying Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to copy data from ...

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 6 - 6?

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.