Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Displaying the First Worksheet in a Macro.
by Allen Wyatt
(last updated August 4, 2016)
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:
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:
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:
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.)
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12271) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Displaying the First Worksheet in a Macro.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
If your macro closes workbooks, you'll want to make sure that it will save any changes you made to the workbook. Here's ...Discover More
When you delete all the macros in a workbook, Excel may still think you have some there. Here's why that happens and what ...Discover More
Want to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.