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

Formatting All Headings At Once

If you need to apply a common formatting change to all the headings in your document, a quick way to do it is to use the ...

Discover More

Hiding Graphics

Do you need a printout where graphics can be turned on and off? This tip provides some concrete ways you can get just ...

Discover More

Adjusting Column Widths on Joined Tables

Each table in a document can have different numbers of columns and different widths for columns. If you want to join two ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Understanding the If ... End If Structure

One of the most basic of programming structures is the conditional structure: If ... End If. This tip explains how this ...

Discover More

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

Highlighting Pattern Violations

A common part of working with text strings in a worksheet is normalizing those strings so that they follow whatever rules ...

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 1 + 1?

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.