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)
For recent versions of Excel, the formula syntax is:
=MID(CELL("filename";A1);FIND("]";CELL("filename";A1))+1;255)
The below will isolate the sheet name:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
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
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)))
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),"?","")
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.
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!
来源:https://stackoverflow.com/questions/28265557/what-excel-formula-returns-the-sheet-name