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)

10

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

Replacing Two Tabs with a Space in Limited Situations

The Find and Replace feature of Word is very powerful, allowing you to finely target exactly what you want to search. ...

Discover More

Adding Headers or Footers to a TOC

Word is very flexible in allowing you to include all sorts of information in a table of contents. This includes ...

Discover More

Formatting Fractions

Need to have a great looking fraction in a document? It's relatively easy to do if you apply the formatting techniques ...

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)

Ordering Worksheets Based on a Cell Value

Need to sort your worksheets so that they appear in an order determined by the value of a cell on each worksheet? Using a ...

Discover More

Retrieving Worksheet Names

Want to grab the names of all the worksheets in a workbook? Here's how you can stuff all those names into the cells of a ...

Discover More

Returning a Worksheet Name

Need to know the name of the current worksheet? You can use the CELL function as the basis for finding this information ...

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 three minus 3?

2018-04-13 04:55:09

Willy Vanhaelen

@Saskia
I tried it and with me the error went on till 29-02-1900. From 01-03-1900 and higher everything is correct. This has to do with the date bug in Excel (inherited from Lotus 123): 1900 is considered to be a leap year and it was not. Try this: enter 60 in a cell and format it as date, you get 29-02-1900 and that date never existed.

Years mutiples of 4 are leap years except when they are a multiple of 100 (like 1900) they are not a leap year except for milleniums, they are leap years (like 2000 was a leap year).

You can fix it by formatting cell A1 as text and then enter the date like 31-01-1900.


2018-04-12 10:21:20

Saskia

The last code works correct except for the first month of 1900.
If the date in A1 (I changed the format in the VBA-code into Dutch code settings: dd-mm-yyyy) is 1-1-1900, the sheetname is 31-12-1899;
if it is 2-1-1900 the sheetname is 1-1-1900;
.... if it is 31-1-1900 the sheetname is 30-1-1900.
But... if it is 1-2-1900 it is correct...!

Why doesn't it work from 1-1-1900 till 31-1-1900??
Greetz Saskia


2018-04-12 04:14:58

Alan Elston

@ Ryan DcMolli
Hi
I am not quite sure if I understand what you are asking for.
The second code form Allen Wyatt changes the tab name of a worksheet automatically when you make an entry in a cell in that worksheet.
You ask this “… When the cell that contains the data, that the Tab data name is pulling from, is changed, can it be updated automatically….” ?? - You are not telling us what should be updated automatically , but if it is the tab name that you mean , then that is exactly what the second code from Allen Wyatt does

You then go on to ask if all the macros can be updated. Once again I am not too sure exactly what you are asking.
But
Maybe this will give you something to play with. The codes are very inefficient, but it might give you some ideas to help achieve whatever it is that you want to do.

I have copied the second code from Allem Wyatt to the first three tabs, which in my German Excel have the code names Tabelle1 , Tabelle2 , Tabelle3 ( For English Excel that will probably be Sheet1, Sheet2, Sheet3 )
Note I am talking here about the code name – In the VBA Project Explorer (usually to the left of your VB Editor Environment ) you will typically see two names for each worksheet like:
Sheet1(Sheet1)
The second one is the tab name and is that which the codes under discussion here will change.
The first one is the code name
(see Figure 1 below)



To copy the codes to the first three worksheet code modules , I followed the steps 1. To 4. From Allen Wyatt. ( I did not do steps 5 to 8 ).

Some modifications are needed to the 3 codes:
In the codes for the second and third worksheet I make a small change:
For both those codes I replace at the start of the code the
Private
with
Public
( This change allows me to access those codes from other code modules. )

For the code in the second tab worksheet code module I change
ActiveSheet.Name
to
Tabelle2.Name

For the code in the third tab worksheet code module I change
ActiveSheet.Name
to
Tabelle3.Name

In the code in the first tab worksheet code module I add two lines. They need to go just before the
Exit Sub
So in other words, the line Exit Sub is replaced by these three lines:
Call Tabelle2.Worksheet_SelectionChange(Tabelle2.Range("A1")) ' Any range will do here as it is not used
Call Tabelle3.Worksheet_SelectionChange(Tabelle3.Range("A1")) ' Any range will do here as it is not used
Exit Sub
I expect that is not doing exactly what you want but you may be able to see from that how to achieve exactly what you want.
Contact me if you want the file I made.
Alan Elston


Figure 1. Code Name(Tab Name)




2018-04-12 03:54:00

Steve Jez

Ryan,
if you add the code to the sheet module, right click on the sheet tab- view code - choose worksheet from the left dropdown & Activate from the right dropdown & paste ActiveSheet.Name = ActiveSheet.Range("A1") at the cursor.

This will run each time the sheet is selected, you will have to select another sheet & then go back to trigger the code.

HTH

Steve


2018-04-11 17:53:00

Ryan DeMolli

Hi Allen,

I used your first macro in your example and it worked fine to change the tab name. However, I have several tabs that I added this Macro to. I am wanting to know if it is possible to have the following. When the cell that contains the data, that the Tab data name is pulling from, is changed, can it be updated automatically. Or can you have another Macro created that will update all of the Macros from all the tabs that are trying to change the Tab name. As of right now I have to go in and run each Macro separately to update the Tab name.

Thanks for you time.

Ryan


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.