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

Creating a Full-Page Border

Borders are easy to add around text or entire paragraphs. It may be surprising to you to learn that Word can just as ...

Discover More

Missing PivotTable Data

Wonder what happened to the data behind a PivotTable? It could be in a number of places, and tracking it down could be a ...

Discover More

Calculated Dates

Word makes it easy to insert today's date in a document, but not as easy to insert a date X number of days in the future. ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Removing a Macro from a Shortcut Key

When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...

Discover More

Copying Worksheets in a Macro

Copying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...

Discover More

Dissecting a String

VBA is a versatile programming language. It is especially good at working with string data. Here are the different VBA ...

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 five more than 3?

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.