excel2007.Tips.Net Welcome toExcel2007.Tips.Net

Helpful Links

Excel 2007 Home
Tips.Net Home

Ask a Question
Make a Comment

Cooking Tips
ExcelTips
Family Tips
Pet Tips

Newest Tips

Changing a Link's Source

Using Conditional Formatting to Shade Rows

Editing by Moving and Copying

Making Sure Duplicate Names Aren't Entered

Applying a Conditional Format to a Full Row

Hiding Worksheet Tabs

Making Cells Flash

 

Running a Macro when Deactivating a Worksheet

Summary: When you change from one worksheet to another, you may want to have Excel automatically run a macro for the worksheet you are leaving. You can easily create such a macro by using the Auto_Deactivate event.

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 is run every time you click on a worksheet tab to leave the current sheet. All you need to do is follow these steps:

  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. (Click here to see a related figure.)
  4. 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.
  5. 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.
  6. Click on the OK button.

Remember that a macro defined in this way 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.

Related Tips:

Find and Replace Almost Anything! An invaluable resource for learning how to harness the full power of Word's search and replace capabilities. You'll discover everything you need in order to master all the intricacies of finding and replacing elements of your document, including the super-powerful "wildcard searches" available in Word. Check out WordTips: Find and Replace today!