What Excel formula returns the sheet name?

我的梦境 提交于 2019-11-30 18:19:19

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)
Orbit

For recent versions of Excel, the formula syntax is:

=MID(CELL("filename";A1);FIND("]";CELL("filename";A1))+1;255)
ADrunkenMan

The below will isolate the sheet name:

=RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename")))
Branston Crumble

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 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),"?","")

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"),"]","")))))
  1. 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.
  2. Uses the number of occurrences of square brackets to substitute the last square bracket with 3 sequential square brackets ']]]'
  3. Uses the Find function to identify the location of the 3 square brackets in the string
  4. Subtracts the location of the 3 square brackets from the length of the full path
  5. 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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!