Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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: Referencing Worksheet Tabs.

Referencing Worksheet Tabs

by Allen Wyatt
(last updated May 30, 2015)

5

Myrna asked if there was a way to use the information in a worksheet tab within a cell. In particular, she named her tabs using dates, and wants to use those dates within the worksheet itself.

There are two ways to go about this. If the names of your worksheet tabs consist only of dates (no other text in them), then you can use the following Excel formula to extract the date:

=MID(CELL("filename"),FIND("]",CELL("filename"),1)+1,10)

This works because =CELL("filename") function returns the complete path and name of the current file along with the text on the worksheet tab. The filename itself appears in square brackets. The formula finds the position of the closing bracket and extracts the first eight characters from that position to the end. (Dates can be expressed in a maximum of 10 characters, as in 12-31-2011.)

One caveat with using this formula is that it only returns anything of value if you first save the workbook. If you use it in a brand new, unsaved workbook, it will return a #VALUE error.

Another approach that is very appealing, particularly if you have additional text in the worksheet tab, is to create a user-defined function. For instance, let's assume that your worksheet tabs have the name "Month Ending 10-31-11". In this case, you could use a function such as the following:

Function SheetName() As Date
    Dim sTab As String
    Application.Volatile
    sTab = ActiveSheet.Name
    sTab = Trim(Right(sTab, 8))
    SheetName = CDate(sTab)
End Function

To use this function in your worksheet, you simply enter the following in a cell:

=SheetName()

The function returns a date serial number, so you will need to format the cell using one of the available date formats. The function works because it assumes that the date is the last 8 characters of the text in the worksheet tab. If your worksheet tabs use a different naming convention (such as placing the date at the beginning of the tab or using 10 digits for the date), then all you need to do is pull the name apart differently in the macro.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6145) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Referencing Worksheet Tabs.

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

Table Numbers are Skipped

What do you do if you add numbered captions to an element of your document (such as tables) and Word skips a number? There ...

Discover More

Ensuring Proper Page Numbers for a Table of Authorities

Automatically create a Table of Authorities entry in your document, and Word might place the necessary field at the wrong ...

Discover More

Managing Corporate Templates

Templates are used to store styles and lots of other customizations that affect how you use Word. On a single-user machine, ...

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)

Referencing a Worksheet Name

Excel provides ways to reference the column or row number of a cell, but it doesn't provide a built-in way to reference a ...

Discover More

Selecting All Visible Worksheets in a Macro

Do you need your macro to select all the visible worksheets (and just the visible ones)? It's not as easy as it sounds, but ...

Discover More

Detecting Types of Sheets in VBA

When processing workbook information in a macro, you may need to step through each worksheet to make some sort of changes. It ...

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 for this tip:

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 nine minus 3?

2015-06-01 13:42:46

Curt

One can use this function/macro to get the sheet name into a cell:

Function shName(rng As Range) As String
Application.Volatile
shName = rng.Worksheet.Name
End Function

... and then put this formula into the desired cell:

=shname(A1)


2015-06-01 05:25:07

DaveS

@Mark
Further to MA's comment, if you omit the ',10' you'll get a 'too few arguments' error.

The value of 10 is not a mistake given the caveats in the article ("If the names of your worksheet tabs consist only of dates (no other text in them)" and "Dates can be expressed in a maximum of 10 characters, as in 12-31-2011."). But I think "extracts the first eight characters" should read "extracts the first ten characters". And if someone uses a format like dd-mmm-yyyy they'll need 11 characters. Using ',31' does make the function generic. The inclusion of $A$1 reference in the CELL function is critical, otherwise the formula will return whichever tab name was most recently changed.


2015-05-31 06:17:30

Michael (Micky) Avidan

@Mark,
Did you managed to use the MID function leaving out the "Num_chars" argument - which, to the best of my knowledge is
not(!) optional.
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


2015-05-30 10:57:40

Mark

Just leave out the ",10" and then it will work for any length name with no margin for error.


2015-05-30 10:07:10

Michael (Micky) Avidan

@@@ To whom it may concern and with all doe respect - I have 2 meaning full comments:
1) The value of 10 (in the MID function) is a mistake, especially if the sheets name exceeds 10 characters(*** by fact it can be up to 31 characters).
2) The lack of the reference to $A$1 will make Excel to misinterpret the formula when copied to other sheets.
Try to enter this tips formula in sheet1 (in cell D5) > copy it and paste in it Sheet2 cell D5.
Check, now, the results, of the formulas, in the two sheets.
So, my suggested formula will look like that:

=MID(CELL("filename",$A$1),FIND("]",CELL("filename",$A$1))+1,31)

Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


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.

Links and Sharing