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.
Written by Allen Wyatt (last updated October 11, 2021)
This tip applies to Excel 2007, 2010, 2013, and 2016
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:
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.
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!
Look at the bottom of a worksheet and chances are you will see tabs for all the worksheets in the current workbook. Want ...
Discover MoreWant a quick way to combine your worksheets? This tip features a simple macro to do the task for you.
Discover MoreIf your workbook contains a multitude of worksheets, the worksheet tabs at the bottom of the program window start to lose ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-09-30 09:44:42
J. Woolley
@Pankaj Sharma
Assuming cell A1 has Alpha, put this formula in cell B1, then copy the formula down column B.
=HYPERLINK("#'"&A1&"'!A1")
Notice the apostrophe on each side of the sheet name in case the name includes a space character.
2021-09-30 04:08:36
Hello Pankaj Sharma … That sort of question, assuming you are asking a question, is probably outside the realms of what Allen Wyatt’s comments section is for.… You probably should post that sort of question at one of the help forums. … mrexcel com or excelforum com are help forums where you will typically get quick answers. ... If you are not in a rush to get an answer, than you can ask at my forum , excelfox com , and I will answer when I have the time……Alan Elston
2021-09-29 17:44:13
Pankaj Sharma
I have an excel document with multiple sheets. They have names:- Master, Alpha, Beta, Gamma,.... so on.
In the master sheet. I have a column COLumn A) which has Alpha, Beta, Gamma... as row entries. I want to add another column (Column B) which has a hyperlink to open different tabs by clicking on it. This column should take the name of the sheet dynamically from Column A.
2021-06-06 11:16:20
Willy Vanhaelen
@Raqune
This tiny one-liner macro should do the job:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then If Len(Target) Then Me.Name = [G4]
End Sub
Be sure to put this macro in the code page of your template worksheet “Blank Invoice”. Right click this sheet’s tab and choose “View Code”. That’s the place to be. When you make a copy of “Blank Invoice” this code is copied along.
“Me” in “Me.Name” stands for “ThisWorksheet”.
[G4] is a shortcut for Range(“G4”).
Let me know if something doesn’t work to your liking.
2021-06-05 14:13:51
Raqune
@Willy Vanhaelen
Date @ G5
=IF(C7="","",NOW()) - enters current date after client name is entered
ClientName @ C7
selected manually from data validation dropdown list
NOTE: Client list for data validation is pulled from "Totals" worksheet ( e.g. =Totals!$A$1:$E$1)
2021-06-02 11:28:49
Willy Vanhaelen
@Raqune
A worksheet event only occurs if you enter something in a cell. When a value shown in a cell changes due to the recalculation of the worksheet, doesn't trigger an event. So, G4 can't trigger an event since it's a formula whose value is calculated. Can you let me know if the values in C7 and/or G5 are entred manually an in what order? If that's the case I can probably help you with a solution.
2021-06-01 00:14:22
Raqune
Allen Wyatt's code worked on a single worksheet, at least at 1st, but if I change a value that causes target cell (in my case G4) to recalculate the tab does not update. If I copy the worksheet with the (somewhat) working code to another worksheet it does not carry the code with it.
I assume steps 5-8, saving a template for Book.xltm & Sheet.xltm in the XLStart direcctory will cause ANY workbook opened to dynamically name workbook tabs whenever "A1" (or whichever cell is designated) is activated with an entry. I want this feature to ONLY work on all sheets in a single given workbook.
I would like to set up only one workbook with a template worksheet (e.g. "Blank Invoice") that I can copy and paste contents to a new worksheet so that each new copy of the original Blank Invoice worksheet will create a new worksheet with dynamic sheet tab naming that will automatically rename tab to reflect invoice number in cell G4.
NOTE: G4 is calculated using Client intiials from C7 & Date from G5 - i.e.calculation in G4 creates a unique invoice number tied to client & date.
Thanks to all for any help!
2020-06-02 03:49:45
@ PRAVEEN…I have done the necessary corrections:…… https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13458&viewfull=1#post13458….BTW….::Allen Wyatt also has a Tip on part 1. of your extended question:…..……. https://excelribbon.tips.net/T013463_Creating_Worksheets_from_a_List_of_Names.html…Alan Elston
2020-06-01 02:37:17
PRAVEEN
Sir, there is a problem in this file...https://app.box.com/s/louq07ga6uth1508e572l7zr9fakont9
When I delete the content of last cell in the list, i.e., A5 (RAHIM), the corresponding sheet "RAHIM" remains visible. The macro to hide the sheet works for all cells, except the last one in the list. I don't know how to fix it.
I also have a small request...
I want to shift the list from Range A1:A5 to Range C4:C9, that is, the list of name will be started from the cell C4 instead of the cell A1.
Would you please make these changes in this excel workbook. (I've tried a lot, but failed everytime)
2020-05-31 06:44:37
SOLVED!Thank you Sir for your effort and time. The solution can be found here..https://excelfox.com/forum/showthread.php/2501-VBA-for-dynamic-sheets-name-dynamic-link-hide-sheets-based-on-a-cell-Value?p=13448#post13448
2020-05-30 10:28:55
J. Woolley
@PRAVEEN
Your question is a good one for the wellsr VBA Q&A site: https://ask.wellsr.com/vba
You might consider posting your question there.
2020-05-29 23:28:19
In simple lines,
I want a workbook which contain
- dynamic sheet name (without going to the sheet (VBA code without "ActiveSheet")
- dynamic hyperlink
- auto hide feature for any sheet
Thanks in advance!
2020-05-29 23:18:07
I have a workbook which contain ONLY 1 sheet named as "Master Sheet". On Master Sheet there is a list of 5 names, as
A1=ANUJ
A2=RITA
A3=MUKESH
A4=RAM
A5=RAHIM
(Actually I have total 400 names, but for easiness I have taken only 5)
Now, my requiremet is:
1. I want to create 5 tabs (Sheets) on the basis of these 5 names. (Now the workbook will have 6 tabs, including Master Sheet)
2. Tab name should be dynamic. Means, if I change the value of A1 from 'ANUJ' to 'SHONA' on Master Sheet, then the "ANUJ" tab should automatically be renamed as "SHONA", without going to the tab "ANUJ". (Because going to 400 tabs to make it "ActiveSheet" is very much time consuming)
3. Each tab should be linked with the corresponding list name, that is, If I click on "A1" (ANUJ) on Master Sheet, the tab (Sheet) "ANUJ" will be opened.
4. Also, this linking should be dynamic, that is, if I change the name "ANUJ" as "SHONA" on Master Sheet, the tab "ANUJ" will be renamed as "SHONA" and remain linked with Cell A1 of Master Sheet.
5. If I delete the content of A1 (ANUJ), i.e, if cell A1 is blank, the corresponding sheet "ANUJ" should hide automatically.
I'm a counsellor and preparing a Record of all students. If someone helps, I'll really be grateful. Please help if possible.
2019-09-08 03:24:56
Brian
so helpful and so easy thanks to your explaination.
2019-07-14 03:26:07
Hello Ruperto,
I am not sure if I fully understand your question. We are not talking about a formula here. We have what is called somethiung like a “Macro” , or “Coding” , or a “routine” , a “Sub routine” , or “event coding” etc..
In your Event coding, the worksheet that has its name changed is the worksheet which is active. This is indicated in your coding by this code part:
ActiveSheet
Putting this in different wording: The tab which has its name changed is the same tab as the one which you are selecting or viewing at the time at which the coding runs. (Your routine runs automatically when a selection change is made).
As an example: If I assume that the other worksheet , ( your “different tab” ) which you want to have its name changed is in the same workbook as the one in which you are selecting, and I will assume that it is the second tab, ( the second worksheet’s name ), whose name you want to change.
The you would replace
ActiveSheet
with something like this:
Worksheets.Item(2)
or
Worksheets.Item(“Sheet2”)
or
Worksheets.Item(“MyCurrentSecondTabName”)
etc..
To explain: If You use a number inside the ( ) , then you are referring to the sheet counting from the left. So the second tab is number 2, the first tab is number 1 etc. If you include a pair of “ “ , and use text inside the (“ “) , then you are referring to the tab by its current tab name. In English Excel , for example, the default tab name for the second worksheet is usually
Sheet2
Alan Elston
2019-07-13 15:18:13
Ruperto
How would I get this formula to refer to a different tab?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Range("J1")
If Target = "" Then Exit Sub
Application.ActiveSheet.Name = VBA.Left(Target, 31)
End Sub
2018-12-19 03:04:01
@ SteveJez
Nice idea to get original name :-)
Alan Elston
2018-12-18 09:56:04
Willy Vanhaelen
@Roy
You are quite right. I have been a little hasty in writing this comment.
In my comments of 12 Dec 2014 and 1 Dec 2018 in tip
https://excelribbon.tips.net/T010942_Dates_Copied_Incorrectly.html
as well as in my comment of 31 Jan 2018 in tip
https://excelribbon.tips.net/T009978_Determining_If_a_Year_is_a_Leap_Year.html
I gave the correct rule.
2018-12-18 03:53:34
SteveJez
tmmcentyre,
Add the OrigName piece of code, amend line 3 of the code as below & add Exit Sub to end of Badname
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Set Target = Range("A1")
If Target = "" Then GoTo OrigName
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
Exit Sub
OrigName:
ActiveSheet.Name = ActiveSheet.CodeName
End Sub
HTH
2018-12-17 12:39:44
Roy
@Willy V: Small thing about leap years. It's "except every four hundred years" rather than on the millenia. (365.2422 days a year so one of every four needs to be a leap year and they picked the one divisible by 400 to be the leap year in each set. Not sure what is intended to fix the extra .0078 days piling up every four hundred years, but... there's some time left to come to a consensus if one hasn't been arrived at yet.)
2018-11-21 09:21:31
tmmcentyre
Thanks for this awesome code! Is it possible that when I delete the contents in A1 the tab will be renamed to the original Sheet# ? Now it just leaves the tab as renamed.
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
@ 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
Working again Today
:)
2018-01-29 03:51:21
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments