Written by Allen Wyatt (last updated November 9, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
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:
Figure 1. The New Name dialog box.
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6138) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Running a Macro When a Worksheet is Deactivated.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
When creating macros, you often need to process numbers in various ways. VBA allows you to convert a numeric value to an ...
Discover MoreWhen you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on ...
Discover MoreYou can manually copy macros from one workbook to another, but what if you want to automate the copying process? Here's ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2025 Sharon Parq Associates, Inc.
Comments