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

Dynamic Worksheet Tab Names

by Allen Wyatt
(last updated November 4, 2017)

5

You probably already know that you can change the name of a worksheet tab by double-clicking on the tab and providing a new name. What if you want to do it dynamically, however? What if you want to have the value in cell A1 automatically appear as the tab name?

Unfortunately, Excel doesn't provide an intrinsic function to handle this sort of task. It is a relatively simple task to develop such a function using a macro that will do the job for you. For instance, the following macro will change the tab name to the contents of A1:

Sub myTabName()
    ActiveSheet.Name = ActiveSheet.Range("A1")
End Sub

There are several important items to note about this macro. First of all, there is no error checking. This means that if A1 contains a value that would be illegal for a tab name then the macro generates an error. Second, the macro must be manually run.

What if you want a more robust macro that does check for errors and runs automatically? The result is a bit longer, but still not overly complex:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Set Target = Range("A1")
    If Target = "" Then Exit Sub
    On Error GoTo Badname
    ActiveSheet.Name = Left(Target, 31)
    Exit Sub
Badname:
    MsgBox "Please revise the entry in A1." & Chr(13) _
    & "It appears to contain one or more " & Chr(13) _
    & "illegal characters." & Chr(13)
    Range("A1").Activate
End Sub

To set up this macro, follow these steps:

  1. Open a new workbook that has only one worksheet in it.
  2. Right-click the worksheet tab and select View Code from the resulting Context menu. Excel displays the VBA Editor.
  3. Paste (or type) the above macro into the code window.
  4. Close the VBA Editor.
  5. Locate the XLStart folder on your system. (Use the Windows search capabilities to locate the folder.)
  6. Save the workbook as an Excel macro-enabled template using the name Book.xltm in the XLStart directory. This causes the template to become your pattern for any new workbook you create.
  7. Again save the workbook as a macro-enabled template in the same directory, this time using the name Sheet.xltm. This causes the template to become the pattern for any new worksheets you insert in a workbook.
  8. Close and restart Excel.

Now, anytime you change the value in cell A1, the worksheet tab also updates.

There is one caveat to using this tip: If the value in cell A1 is a date and you want the worksheet tab to contain that date, then you may not get what you expect. The reason is simple: Excel stores dates internally as serial numbers, and that is what gets assigned to the worksheet tab, not a formatted date. If you are working with dates, then you'll need to change what actually is assigned to the tab name:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Set Target = Range("A1")
    If Target = "" Then Exit Sub
    On Error GoTo Badname
    ActiveSheet.Name = Format(Target, "mmm-dd-yy")
    Exit Sub
Badname:
    MsgBox "Please revise the entry in A1." & Chr(13) _
    & "It appears to contain one or more " & Chr(13) _
    & "illegal characters." & Chr(13)
    Range("A1").Activate
End Sub

Note that the only change here is what is assigned to the worksheet's Name property—it is a formatted date. You can, if you prefer, modify the date format used in the macro. You should not, however, choose a format that uses slashes because those are illegal in worksheet names.

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

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

Remembering Copies to Print

If you routinely need to print more than one copy of a document, you'll love the ideas presented in this tip. There's ...

Discover More

Pasting a Hyperlink

Need a quick link within a document to some external data? You can paste information so that Excel treats it just like a ...

Discover More

Best Quality for High Resolution Graphics

You want your documents to look as good as they can. If those documents include graphics, then you also need to make sure ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Creating a Worksheet Copy by Default

Excel makes it relatively easy to copy worksheets to a different workbook. That doesn't mean it couldn't be made simpler ...

Discover More

Finding a Worksheet with a Specific Value in a Specific Cell

If you have a lot of worksheets in workbook, finding the exact one you want can be a bit tricky. This tip looks at ...

Discover More

Testing for an Empty Worksheet

If you are using a macro to process a number of worksheets, you may have a need to know if the worksheet is empty or not. ...

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}] 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 less than 7?

2018-01-30 03:05:34

Alan Elston

Working again Today
:)


2018-01-29 03:51:21

Alan Elston

Just adding a comment so that I can try to get “Notify me about new comments ANYWHERE ON THIS SITE” working again for me.. it stopped working for me a few days ago, that is to say I suddenly stopped getting E-Mail notifications??
Alan Elston


2017-11-07 12:29:14

Manfred Kohler

@Michael and Willy: Both macros fails if the entry in A1 is equal for several worksheets. A combination of both can solve the problem

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Source: https://excelribbon.tips.net/T007993_Dynamic_Worksheet_Tab_Names.html

If Target.Address <> "$A$1" Then Exit Sub
On Error GoTo Badname
ActiveSheet.Name = IsValid(Left(Target, 31))
Exit Sub
Badname:
MsgBox "Please revise the entry in A1." & Chr(13) _
& "It contains same entry as in another worksheet (must be unique)."
End Sub

Function IsValid(ByVal FN As String) As String
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.Pattern = "[]\\/:\*\?<>""\|]"
IsValid = RegEx.Replace(FN, "") 'This will change the name of the WS, but does not affect the entry in A1
'Range("A1") = IsValid 'will end up with an endless loop
'If Range("A1") <> IsValid Then Range("A1") = IsValid 'should work
End Function


2017-11-06 02:56:16

Michael (Micky) Avidan

I would suggest to convert the illegal name to something legal.
--------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then Exit Sub
ActiveSheet.Name = IsValid(Target.Value)
End Sub

Function IsValid(ByVal FN As String) As String
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.Pattern = "[]\\/:\*\?<>""\|]"
IsValid = RegEx.Replace(FN, "")
End Function
------------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” Excel MVP – Excel (2009-2018)
ISRAEL


2017-11-05 11:30:39

Willy Vanhaelen

The macros in this tip are badly designed.
The use of the Worksheet_SelectionChange event is not appropriate in this case.

Every time the cell pointer is moved or an entry is made in any cell the macro runs and the sheet's name is changed over and over again although it only has to be changes if the contents of A1 changes.

You can try it by inserting a Beep command before the "On Error GoTo Badname" line. Move around the sheet and enter anything in some random cells (except A1). Every time you hear the beep sound the sheet's name has been changed to the content of A1 although A1 isn't touched. What a waste !!!

The event to be used here is Worksheet_Change. The following macro is far more efficient and changes the sheet's name only if A1 is changed:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address <> "$A$1" Then Exit Sub
On Error GoTo Badname
ActiveSheet.Name = Left(Target, 31)
Exit Sub
Badname:
MsgBox "Please revise the entry in A1." & Chr(13) _
& "It appears to contain one or more illegal characters."
End Sub

There is no need for a "date version" of the macro because Excel detects when a date is entered in A1 and automatically formats the cell accordingly.


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.