Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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: Displaying the First Worksheet in a Macro.

Displaying the First Worksheet in a Macro

Written by Allen Wyatt (last updated July 2, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


Terri has a macro that runs automatically when a worksheet is opened. One thing it does is to display the first worksheet in the workbook, which is usually called "Consolidated." This works great, unless the first worksheet doesn't have this name. Then Terri must remember to change the macro to specify a different worksheet name. She wonders if there is a way to write her macro so that the first worksheet is always displayed, regardless of its name?

Normally, as Terri alludes to, you would display a given worksheet by using its name in the statement, in this manner:

Worksheets("Consolidated").Activate

This works great, as long as there is a worksheet by this name (Consolidated) in the workbook. Displaying a particular worksheet (like the first one in the workbook) when you don't know what the name of that worksheet might be takes a different approach.

The simple answer is to start referring to the worksheet using its position within the Worksheets collection. All the worksheets in a workbook belong to a collection of worksheet objects. This collection is (oddly enough) referred to as the Worksheets collection. You can refer to an individual worksheet in the collection by name (as was done in the previous example) or you can refer to them by using an index number within the collection. For instance, you can activate the first worksheet in the collection in this manner:

Worksheets(1).Activate

Using this method, it really doesn't matter what the name of the first worksheet is; it could easily be "Consolidated" or some other name. Excel dutifully activates the first worksheet in the workbook.

The only time this wouldn't work is if the first worksheet in your workbook is hidden. If the worksheet is not visible, then Excel automatically (after execution of this statement) displays the first visible worksheet.

Note that this displays the first (leftmost) worksheet tab in the workbook. If you instead want to display the first created worksheet in a workbook, regardless of its position, you can try a different approach. Each worksheet has (for lack of a better term) a behind-the-scenes "code name." These code names should sound familiar; they are Sheet1, Sheet2, Sheet3, etc. These names are retained even though you may change the name of the worksheet itself or change the position of the tabs. If you want to display the first worksheet created (again, regardless of position), you could try the following:

Sheet1.Activate

There is one caveat to this: It is possible that the code name for your worksheets has been changed, if you write the programming code to do so. If that is the case, then the above statement may not provide the results desired. (Testing is always a good idea.)

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 (12272) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Displaying the First Worksheet in a Macro.

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

Controlling How Word Sorts Text

Word has a very orderly way in which it sorts information, but that orderly method may not meet what you need to have ...

Discover More

Making Macros Readily Available

Want to get at your macros quickly? One way is to create a Macros menu to make your most common macros immediately available.

Discover More

Duplex by Default

Many printers these days have the capability to print on both sides of a piece of paper. You may want Word to use this ...

Discover More

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!

More ExcelTips (ribbon)

Stopping Fonts from Changing

There are multiple ways that Excel can create a workbook, and the formatting in each creation method can be different. In ...

Discover More

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...

Discover More

Conditionally Displaying a Message Box

You can, from within your macros, easily display a message box containing a message of your choice. If you want to ...

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 two less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.