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 Activated.

Running a Macro when a Worksheet is Activated

Written by Allen Wyatt (last updated February 8, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

Excel allows you to define macros that are executed whenever certain well-defined events occur in the Excel environment. One of those events is when a worksheet is activated. For instance, you could create a macro that defined a custom menu structure whenever a particular worksheet is displayed.

In order to create a macro that runs when a worksheet is activated, follow these steps:

  1. Display the VBA Editor by pressing Alt+F11.
  2. In the Project window, at the left side of the Editor, double-click on the name of the worksheet that you want to affect. (You may need to first open the VBAProject folder, and then open the Microsoft Excel Objects folder under it.) The code window for the worksheet should appear.
  3. In the code window, use the right pull-down list (there are two pull-down lists just below the title bar of the code window) to select the Activate option. The following code should appear in the code window:
     Private Sub Worksheet_Activate()

     End Sub
  1. In this new subroutine enter the macro you want executed whenever the worksheet is activated.
  2. Close the VBA Editor.

Remember that a macro defined in this way is run every time the worksheet is activated, not just the first time. Think about how you use Excel; it is possible to activate 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 (7221) 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 Activated.

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

Inserting the User's Name

Word keeps track of a name for the person using the program. If you want to add this person's name into the document, ...

Discover More

Setting Page Margins

When getting ready to print your worksheet, you may want to take a moment to check what margins Excel will use on the ...

Discover More

Inserting Foreign Characters

It is not unusual to need to insert foreign characters (often called diacritical marks) as part of your typing. Word ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Importing Based on a Partial File Name

A common task for macros is to open and process a file you want imported into your workbook. If you need to identify the ...

Discover More

Pausing Macros for User Input

Does your macro need to get some input from a user? Here are the ways that Excel provides for that input to be solicited.

Discover More

Counting All Characters

Need to know how many characters there are in a workbook? You can find out easily with the handy macro introduced 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 8 - 5?

2020-07-19 08:33:02

Peter Atherton

Eddie D Faucheux

It is entered in the Worksheet Code. Right-click the sheet tab and pick View Code the code shound be triggered by the sheet Activevate event

Private Sub Worksheet_Activate()
myMacro
End Sub


2020-07-18 13:16:06

Eddie D Faucheux

In this new subroutine enter the macro you want executed whenever the worksheet is activated.

Where do you enter it?


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.