When working with Excel spreadsheets, sometimes you may have a need to get the name of the worksheet.
While you can always manually enter the sheet name, it won’t update in case the sheet name is changed.
So if you want to get the sheet name, so that it automatically updates when the name is changed, you can use a simple formula in Excel.
In this tutorial, I will show you how to get the sheet name in Excel using a simple formula.
Get Sheet Name Using the CELL Function
CELL function in Excel allows you to quickly get information about the cell in which the function is used.
This function also allows us to get the entire file name as a result of the formula.
Suppose I have an Excel workbook with the sheet name ‘Sales Data’
Below is the formula that I have used in any cells in the ‘Sales Data’ worksheet:
=CELL("filename"))
As you can see, it gave me the whole address of the file in which I am using this formula.
But I needed only the sheet name, not the whole file address,
Well, to get the sheet name only, we will have to use this formula along with some other text formulas, so that it can extract only the sheet name.
Below is the formula that will give you only the sheet name when you use it in any cell in that sheet:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
The above formula will give us the sheet name in all scenarios. And the best part is that it would automatically update in case you change the sheet name or the file name.
Note that the CELL formula only works if you have saved the workbook. If you haven’t, then it would return a blank (as it has no idea what the workbook path is)
Wondering how this formula works? Let me explain!
The CELL formula gives us the whole workbook address along with the sheet name at the end.
One rule it would always follow is to have the sheet name after the square bracket (]).
Knowing this, we can find out the position of the square bracket, and then extract everything after it (which would be the sheet name)
And that’s exactly what this formula does.
The FIND part of the formula looks for ‘]’ and return it’s position (which is a number that denotes the number of characters after which the square bracket is found)
We use this position of the square bracket within the RIGHT formula to extract everything after that square bracket
One major issue with the CELL formula is that it’s dynamic. So if you use it in Sheet1 and then go to Sheet2, the formula in Sheet1 would update and show you the name as Sheet2 (despite the formula being on Sheet1). This happens as the CELL formula considers the cell in the active sheet and gives the name for that sheet, no matter where it is in the workbook. A workaround would be to hit the F9 key when you want to update the CELL formula in the active sheet. This will force a recalculation.
Alternative Formula to Get Sheet Name (MID formula)
There are many different ways to do the same thing in Excel. And in this case, there is another formula that works just as well.
Instead of the RIGHT function, it uses the MID function.
Below is the formula:
=MID(CELL("filename"),FIND("]",CELL("filename"))+1,255)
This formula works similarly to the RIGHT formula, where it first finds the position of the square bracket (using the FIND function).
It then uses the MID function to extract everything after the square bracket.
Fetching Sheet Name and Adding Text to it
If you’re building a dashboard, you may want to not just get the name of the worksheet, but also append a text before or after it.
For example, if you have a sheet name 2021, you may want to get the result as ‘Summary of 2021’ (and not just the sheet name).
This can easily be done by combining the formula we saw above with the text we want before it using the ampersand operator.
Below is the formula that will add the text ‘Summary of ‘ before the sheet name:
="Summary of "&RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
The ampersand operator (&) simply combines the text before the formula with the result of the formula. You can also use the CONCAT or CONCATENATE function instead of an ampersand.
Similarly, if you want to add any text after the formula, you can use the same ampersand logic (i.e., have the ampersand after the formula followed by the text that you want to append).
So these are two simple formulas that you can use to get the sheet name in Excel.
I hope you found this tutorial useful.
Other Excel tutorials you may also like:
- How to Rename a Sheet in Excel (4 Easy Ways + Shortcut)
- How to Insert New Worksheet in Excel (Easy Shortcuts)
- How to Unhide Sheets in Excel (All In One Go)
- How to Sort Worksheets in Excel using VBA (alphabetically)
- Combine Data From Multiple Worksheets into a Single Worksheet in Excel
- How to Compare Two Excel Sheets
- How to Group Worksheets in Excel
Return to Excel Formulas List
Download Example Workbook
Download the example workbook
This tutorial will demonstrate how to get a sheet name with a formula in Excel.
Get Sheet Name
In Excel there isn’t any one function to get the sheet name directly. But you can get a sheet name using VBA, or you can use the CELL, FIND, and MID functions:
=MID(CELL("filename"),FIND("]",CELL("filename"))+1,31)
Let’s go through the above formula.
CELL Function
We can use the CELL Function to return the file path, name, and sheet by inputting “filename”.
=CELL("filename")
To get the current worksheet’s name, you can use the function with or without the optional reference argument, referring to any cell on the current tab.
=CELL("filename",A1)
You can also get information about any other worksheet by referring to a cell on that sheet.
=CELL("filename",AnotherSheet!A1)
FIND Function
As shown above, the CELL Function returns the file path, name, and worksheet. We don’t need the path or workbook name, so we need to isolate the sheet name.
First use the FIND Function to determine the position of the character immediately before the sheet name (which is always “]”).
=FIND("]",B3)
MID Function
Since we have the last character position before the worksheet name, we can now use the MID Function to extract the characters after that position. Our sheet name starts at the position found above + 1, but we don’t know how long the sheet name is. However, we do know the maximum length of a worksheet name (31), and we can use that in the MID Function:
=MID(B3,C3+1,31))
Putting this all together yields our original formula:
=MID(CELL("filename"),FIND("]",CELL("filename"))+1,31)
Often, you need to insert and work with the sheet name in an Excel sheet, for example if you are working with the ‘INDIRECT’-formula. Or, if you want to dynamically change headlines depending on the sheet name. If you don’t want to type the sheet name manually – which is very unstable – there are three ways to get a sheet name
Before we start: If you just have to insert the sheet name for a small amount of worksheets, please consider doing it manually. It usually is the fastest way.
Method 1: Insert the sheet name using built-in Excel functions
The easiest way is to copy the following function and paste it into your Excel cell:
=RIGHT(CELL("filename",A1),LEN(CELL("filename",A1))-SEARCH("]",CELL("filename",A1)))
This formula obtains the filename “=CELL(“filename”,A1)” and separates it after the character “]”. If you want to get the name of another Excel sheet, you have to change the cell reference from “A1” to any cell of the other worksheet. And depending on your version and language of Excel, you might have to translate the function names and maybe replace “,” by “;”.
The big advantages of this method is that it doesn’t require any programming in VBA or a third-party Excel add-in.
On the downside, please note the following comments:
- If you open a new file and paste this function, it won’t work before saving it.
- The cell function is volatile. That means, it always calculates no matter if you’ve changed anything. This is a disadvantage for large Excel files where the performance of calculation is crucial.
- Also, the cell function doesn’t translate to other languages. If your Excel is set to German, Spanish etc., you have to replace the “filename” part with the respective word in your language.
Do you want to boost your productivity in Excel?
Get the Professor Excel ribbon!
Add more than 120 great features to Excel!
Method 2: Return the sheet name using VBA
The second way to get a worksheet name uses VBA. Once set-up, it might be easier to use. You have to open the developer tools and add a module. Next, you can use this code:
Function PROFEXSheetName(cell As Range) As String
'This function returns the name of a selected worksheet
'"cell" contains the link to a cell on the worksheet
Application.Volatile
'With "Application.Volatile" you can make sure, that the function will be recalculated once the worksheet is recalculated
PROFEXSheetName = cell.Worksheet.Name
'Returns the worksheet name
End Function
Now, you can use the function “=PROFEXSheetName(A1)” in your Excel cell and the worksheet name will be shown.
As the result of both ways is (or should be…) the same, it is up to you which way to choose. If you feel comfortable using an easy macro, you probably go for the second way. If not, just paste the formula from the first way to your cell.
Method 3: Use ‘Professor Excel Tools’
Because returning the sheet name is a common problem in Excel, it’s included in the Excel add-in ‘Professor Excel Tools’. Just type =PROFEXSheetName(A1) and it’ll return the sheet name. Download the free trial below.
Please note: for using this feature of Professor Excel Tools, you don’t have to buy a license. The built-in Excel functions are free to use in order to guarantee compatibility.
This function is included in our Excel Add-In ‘Professor Excel Tools’
(No sign-up, download starts directly)
Example: Take it a step further: Insert and clean a worksheet name
I usually recommend not using space characters in worksheet names. It has some disadvantages, especially when linking to other worksheets (read more about worksheet names here).
So, I usually use underscore “_”-characters to separate words in sheet names. Let’s assume the following example: The worksheet name should at the same time be displayed in a heading. Here is how to do it, using the method one from above.
The approach: Wrap the substitute function around the sheet name function:
So, the function is:
=SUBSTITUTE(RIGHT(CELL("filename",A1),LEN(CELL("filename",A1))-SEARCH("]",CELL("filename",A1))),"_"," ")
Download example sheet and further reading
Please download the example file here. It contains examples for the methods two and three from above.
You don’t want to insert the worksheet name but rather the whole path, file name etc.? Check this article. It has everything, you need!
In this example, the goal is to return the name of the current worksheet (i.e. tab) in the current workbook with a formula. This is a simple problem in the latest version of Excel, which provides the TEXTAFTER function. In older versions of Excel, you can use an alternative formula based on the MID and FIND functions. Both formula options rely on the CELL function to get a full path to the current workbook. Read below for a full explanation.
Workbook path
The first step in this problem is to get the workbook path, which includes the workbook and worksheet name. This can be done with the CELL function like this:
CELL("filename",A1)
With the info_type argument set to «filename», and reference set to cell A1 in the current worksheet, the result from CELL is a full path as a text string like this:
"C:pathtofolder[workbook.xlsx]sheetname"
Notice the sheet name begins after the closing square bracket («]»). The problem now becomes how to extract the sheet name from the path? The best way to do this depends on your Excel version. Use the TEXTAFTER function if available. Otherwise, use the MID and FIND functions as explained below.
TEXTAFTER function
In Excel 365, the easiest option is to use the TEXTAFTER function with the CELL function like this:
=TEXTAFTER(CELL("filename",A1),"]")
The CELL function returns the full path to the current workbook as explained above, and this text string is delivered to TEXTAFTER as the text argument. Delimiter is set to «]» in order to retrieve only text that occurs after the closing square bracket («]»). In the example shown, the final result is «September» the name of the current worksheet in the workbook shown.
MID + FIND function
In older versions of Excel that do not offer the TEXTAFTER function, you can use the MID function with the FIND function to extract the sheet name:
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)
The core of this formula is the MID function, which is used to extract text starting at a specific position in a text string. Working from the inside out, the first CELL function returns the full path to the current workbook to the MID function as the text argument:
CELL("filename",A1) // get full path
We then need to tell MID where to start extracting text. To do this, we use the FIND function with a second call to the CELL function to locate the «]» character. We want MID to start extracting text after the «]» character, so we use the FIND function to get the position, then add 1:
FIND("]",CELL("filename",A1))+1 // get start number
The result from the above snippet is returned to the MID function as start_num. For the num_chars argument, we hard-code the number 255*. The MID function doesn’t care if the number of characters requested is larger than the length of the remaining text, it simply extracts all remaining text. The final result is «September» the name of the current worksheet in the workbook shown.
*Note: In Excel user interface, you can’t name a worksheet longer than 31 characters, but the file format itself permits worksheet names up to 255 characters, so this ensures the entire name is retrieved.
In this post, you’ll learn about with a detailed explanation of how to get sheet name in Microsoft Excel using VBA.
Table of Contents
- How to Get Sheet Name in Excel VBA?
- Get ActiveSheet Name
- Get Sheet Name by index Number
- Get Sheet Name by Code Name
- Check if Sheet Name Exists
Sheet names are stored in the Name property of the Sheets or Worksheets object. The Sheet Name is the “tab” name that’s visible at the bottom of Excel.
Get ActiveSheet Name
To display the ActiveSheet name in a message box, use the below code snippet
Get Sheet Name by index Number
To display the worksheet name in a message box by its index number:
To display the name of the last worksheet in the workbook:
MsgBox Sheets(Sheets.Count).Name
Get Sheet Name by Code Name
In the VBA Editor, there is an option to change the “code name” of a Sheet. The code name is not visible to the Excel user and can only be seen in the VBA Editor:
The VBA code name:
To get the Sheet name in a MsgBox using the VBA Code name:
Check if Sheet Name Exists
This is used to check whether the sheet name exists already.
Enter the following code in the module and click run
Code:
Function WorksheetExists2(WorksheetName As String, Optional wb As Workbook) As Boolean If wb Is Nothing Then Set wb = ThisWorkbook With wb On Error Resume Next WorksheetExists2 = (.Sheets(WorksheetName).Name = WorksheetName) On Error GoTo 0 End With End Function Sub FindSheet() If WorksheetExists2("Sheet1") Then MsgBox "Sheet1 is in this workbook" Else MsgBox "Oops: Sheet does not exist" End If End Sub
I have searched the excel function documentation and general MSDN search but have been unable to find a way to return the sheet name without VBA.
Is there a way to get the sheet name in an excel formula without needing to resort to VBA?
pnuts
58k11 gold badges85 silver badges137 bronze badges
asked Feb 1, 2015 at 17:27
Not very good with excel, but I found these here
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)
and A1
can be any non-error cell in the sheet.
For the full path and name of the sheet, use
=CELL("filename",A1)
answered Feb 1, 2015 at 17:31
TravisTravis
1,2641 gold badge16 silver badges33 bronze badges
4
Here’s a reasonably short one that has a couple added benefits:
-
Does a reverse lookup (most other answers go wrong direction) by using the often ignored
REPT
function. -
A1
seems like a poor choice as there’s a considerably higher chance iterrors
compared to…$FZZ$999999
. -
Don’t forget to absolute. Copying and pasting some of the other examples could error due to referential changes.
-
The
?
is intentional as that shouldn’t be in the file path.=SUBSTITUTE(RIGHT(SUBSTITUTE(CELL("Filename",$FZZ$999999), "]",REPT("?", 999)), 999),"?","")
answered Jun 20, 2019 at 0:38
pgSystemTesterpgSystemTester
8,7802 gold badges22 silver badges49 bronze badges
The below will isolate the sheet name:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
eli-k
10.7k11 gold badges43 silver badges44 bronze badges
answered Aug 8, 2016 at 3:52
2
For recent versions of Excel, the formula syntax is:
=MID(CELL("filename";A1);FIND("]";CELL("filename";A1))+1;255)
Wizhi
6,3684 gold badges24 silver badges47 bronze badges
answered Oct 17, 2018 at 10:59
OrbitOrbit
2121 silver badge7 bronze badges
None of the formulas given in other answers supports the case if there is character ] in filepath.
The formula below is more complex, but it can handle such cases properly:
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1),FIND("?",SUBSTITUTE(CELL("filename",A1),"","?",LEN(CELL("filename",A1))-LEN(SUBSTITUTE(CELL("filename",A1),"","")))))+1,LEN(CELL("filename",A1)))
answered Apr 19, 2019 at 1:47
mielkmielk
3,88012 silver badges19 bronze badges
0
I had a module already open so I made a custom function:
Public Function Sheetname (ByRef acell as Range) as string
Sheetname = acell.Parent.Name
End Function
eli-k
10.7k11 gold badges43 silver badges44 bronze badges
answered Dec 21, 2017 at 15:00
3
Had a square bracket ‘]’ in the file name, so needed to modify the above formula as per the following to find the last occurrence of the square bracket.
Verified that this works for 0,1 or more square brackets in the file name / path.
=RIGHT(CELL("filename"),LEN(CELL("filename")) - FIND("]]]",SUBSTITUTE(CELL("filename"),"]","]]]",LEN(CELL("filename"))-LEN(SUBSTITUTE(CELL("filename"),"]","")))))
- Replaces all square brackets using the substitute function, then compares the length of the result with the length of the file name to identify the number of square brackets in the file name.
- Uses the number of occurrences of square brackets to substitute the last square bracket with 3 sequential square brackets ‘]]]’
- Uses the Find function to identify the location of the 3 square brackets in the string
- Subtracts the location of the 3 square brackets from the length of the full path
- Uses the result to get the right most characters (being the sheet name)
Previous comment above about saving the workbook first is also a key, as you’ll otherwise receive the #Value! result.
answered Jun 14, 2019 at 2:27
JokeyJokey
313 bronze badges
The below works for me and is simpler (to me at least) than other solutions, while still handling a square bracket in the file name:
=MID(CELL("filename", A1),6+SEARCH(".xlsx]",CELL("filename", A1)),32)
This assumes it is an «xlsx» file and that the filename will not contain «.xlsx]» in addition to the xlsx suffix, which in my case is an assumption that is safe enough to make.
If you wanted to handle both «.xlsx» and «.xls» file names, you could use:
=MID(SUBSTITUTE(CELL("filename", A1),".xls]",".xlsx]"),6+SEARCH(".xlsx]",SUBSTITUTE(CELL("filename", A1),".xls]",".xlsx]")),32)
answered Dec 26, 2020 at 21:19
mwagmwag
3,36929 silver badges36 bronze badges
I’m pretty sure you could have Googled this. I just did, and here is the very first thing that came up for me.
In Excel it is possible to use the CELL function/formula and the MID and FIND to return the name of an Excel Worksheet in a Workbook. The formula below shows us how;
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,256)
Where A1 is any non error cell on the Worksheet. If you want the full path of the Excel Workbook, simply use;
=CELL("filename",A1)
The only catch is that you have to save the file for this to work!
answered Feb 8, 2017 at 21:42
ASHASH
20.2k18 gold badges80 silver badges183 bronze badges
0
To return the sheet name in a cell, use CELL, FIND and MID in Excel. There’s no built-in function in Excel that can get the sheet name.
1. The CELL function below returns the complete path, workbook name and current worksheet name.
Note: instead of using A1, you can refer to any cell on the first worksheet to get the name of this worksheet.
2. Use the FIND function to find the position of the right bracket. Add 1 to return the start position of the sheet name.
3. To extract a substring, starting in the middle of a string, use the MID function. First argument (formula from step 1). Second argument (formula from step 2). Third argument (31).
Explanation: the MID function shown above starts at position 24 and extracts 31 characters (maximum length of a worksheet name).
4. You may want to add text to the name of the sheet. Simply use the ampersand (&) operator as shown below.
5. To get the name of the second worksheet, simply refer to any cell on the second worksheet.
Pro tip: use Excel VBA to display the sheet names of all Excel files in a directory. You can find detailed instructions here.
Home/ Formulas/ Excel Formula to Display the Sheet Name in a Cell
This blog post looks at using an Excel formula to display the sheet name in a cell. By finding the sheet name using an Excel formula, it ensures that if the sheet name is changed, the formula returns the new sheet name.
For the formula we will be using the CELL, MID and FIND functions. Let’s begin by looking at the CELL function.
The CELL function is a fantastic, and relatively unknown, function in Excel. Its purpose is to return information about a cell such as its column, cell colour, whether it is in a protected state or the filename. We need to use the function to return the filename of a cell. Because our goal is to return the sheet name, it does not matter what cell we use. Any cell on the sheet will work. In the example below, cell B1 has been used.
=CELL("filename",B1)
The function above will return the full filename of the cell such as;
C:UsersTrainee1Desktop[return-sheet-name.xlsx]London
Returning Just the Sheet Name to a Cell
Now that we have the filename, we need to extract just the sheet name. The MID and FIND functions will be used for this.
The FIND function is used to return the position of the closing “]” + 1. The “]” indicates the end of the workbook reference, so the following characters are those for the sheet name.
The MID function then extracts up to the next 32 characters. This is an excessive number but because sheet names cannot have more than 31 characters it ensures the full name is returned whatever it may be.
Here is the full Excel formula to display the sheet name in a cell;
=MID(CELL("filename",B1),FIND("]",CELL("filename",B1))+1,32)