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?
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?
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)
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 it errors
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),"?","")
The below will isolate the sheet name:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
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)))
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
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"),"]","")))))
Previous comment above about saving the workbook first is also a key, as you'll otherwise receive the #Value! result.
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)
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!