Forum Discussion
how to reference the current sheet name in an function?
I am aware that you can reference other sheets in a function, but how do you reference the current sheet in one?
IF(Current_sheet_name="user template", value_if_true, value_if_false)
Thanks!
(the workbook must be saved)
If you run Excel 365:
=IF(TEXTAFTER(CELL("filename",A1),"]") = "user template", value_if_true, value_if_false)
with other versions:
=IF(RIGHT(CELL("filename",A1),LEN(CELL("filename",A1))-SEARCH("]",CELL("filename",A1))) = "user template", value_if_true, value_if_false)
- RichHoltonCopper Contributor
There is a subtle weakness to all of the solutions so far: they all assume that the filename and path do not contain the right square bracket ("]"). But in both Windows and Mac (OS X and later), filenames and paths can contain that character.
However, with the new text functions in Excel 365 and Excel for the Web, you can fix this weakness and greatly simplify the formula:
=TEXTAFTER(CELL("filename",A1),"]",-1)
The TEXTAFTER function can be very useful for this sort of thing.
- dti65Copper Contributor
RichHoltonHowever the new functions in Excel solve a lot, there is still a certain weakness in this formula, if you have multiple worksheets of which you'd want to use the name, excel states the name of the last active worksheet is used all over the workbook. Better would be when Microsoft would add "worksheetname" in the possible list of "infotypes" and makes is worksheet dependent.
- SparksnFlashCopper Contributor
Nope, the question was if we are in right sheet or not. All answers assume we have "]" at the right. Compare your formula with one from Lorenzo above.
- RichHoltonCopper ContributorThe assumption is not only that we have "]" at the right. It's that the first instance of "]" is the separator between the path/filename and the sheet name.
But it's possible to have a path and/or filename that contains the "]" character. So, CELL("filename") could return something like
"D:\reports\reports [for review]\July report [new].xlsx]user template".
With that path/filename, the formulas in Lorenzo above will return the sheet name as
"\July report [new].xlsx]user template",
which would not be equal to "user template".
My suggested formula instead searches for the final "]". Since Excel doesn't allow that character in sheet names, the final "]" will always be the separator between the path/filename and the sheetname, regardless of any that might be in the path/filename.
- LiamSintonCopper Contributor
RichHolton Brilliant, after a lot of convoluted attempts (all beyond my knowledge though tbf), this worked a treat 👍
- marrionberry8848Copper Contributor
Needsleep =(MID(@CELL("filename",A1),FIND("]",@CELL("filename",A1))+1,256)) try this one; it worked for me
- SparksnFlashCopper ContributorThis is the one!! Thanks
- RiverLinkCopper Contributor
worked for me too ... thanks !!
- NikolinoDEGold Contributor
You can use the CELL function to reference the current sheet name in an Excel function.
Here’s how:
- Click on the cell where you want to reference the current sheet name.
- Type =CELL("filename") in the formula bar and press Enter.
- The result will be a string that contains the full path and name of the workbook, followed by a square bracket, followed by the sheet name in quotes. For example: [Book1.xlsx]Sheet1.
- To extract just the sheet name from this string, you can use the MID function. Here’s an example formula that returns the current sheet name: =MID(CELL("filename"),FIND("]",CELL("filename"))+1,255).
- Now you can use this formula to reference the current sheet name in your function like this: =IF(MID(CELL(\"filename\"),FIND(\"]\",CELL(\"filename\"))+1,255)=\"user template\", value_if_true, value_if_false).
I hope this helps!
- RickSeidenCopper Contributor
If you use the CELL function in more than one sheet in your workbook, they will all update to the match the last one you use.
So if you have Sheet1 and Sheet2 and put
=CELL("filename")
in Sheet1!A1 it will show "Sheet1" in that cell. Then if you go to Sheet 2 and put the same thing in cell Sheet2!A1, it will show "Sheet2" there. But if you then went back to Sheet1, Sheet1!A1 will now show "Sheet2".
That's why it shall be used with reference like in Lorenzo post.
- LorenzoSilver Contributor
(the workbook must be saved)
If you run Excel 365:
=IF(TEXTAFTER(CELL("filename",A1),"]") = "user template", value_if_true, value_if_false)
with other versions:
=IF(RIGHT(CELL("filename",A1),LEN(CELL("filename",A1))-SEARCH("]",CELL("filename",A1))) = "user template", value_if_true, value_if_false)
- Aurimas444Copper Contributor
Lorenzo solution will not work if worksheet will be uploaded to Sharepoint and opened from browser:
"filename"
Filename (including full path) of the file that contains reference, as text. Returns empty text ("") if the worksheet that contains reference has not yet been saved.
Note: This value is not supported in Excel for the web, Excel Mobile, and Excel Starter.
- JimS54Copper Contributor
Lorenzo As far as I can see, it's still the case that neither of these work within Excel for the Web (sorry for swearing...) which I unfortunately need to use in my work SharePoint environment. Presumably because the CELL() function and "filename" variable aren't available in EftW.
Most of the solutions I've seen suggested to date are workarounds based on extracting the sheet / tab name from the "filename" string - which won't work on EftW; or FORMULATEXT / SUBSTITUTE formulae which depend on extracting the sheet / tab name from cross-references between sheets, which I'm finding difficult to set up in such a way that those who aren't Excel gurus can replicate when new tabs are added in the future.
Can anyone explain why it would be so difficult for Microsoft (or someone whose knowledge of Excel is a lot greater than mine) to create a function which directly extracts a sheet name from wherever the tab text is stored within the file, rather than us having to resort to indirect workarounds? Something nice and simple like SHEETNAME(Sheet), where Sheet is the Sheet Number, or the current sheet if omitted...
- LorenzoSilver Contributor
IMHO the only thing we can do is upvote Create a function that returns the list of sheet name(s) in the workbook = SHEETNAME(), =SHEETNAMES() until there's enough to get MSFT's attention. Then, cross-fingers...
- tomspoorsCopper Contributor
This worked for me:
=RIGHT(CELL("filename"),LEN(CELL("filename"))-SEARCH("]",CELL("filename")))
The right square bracket appears only once in the returned filename string.
Tom
- colinb500Copper ContributorI did not underetand.
- sethkirk26Copper Contributor
Here is a much more clean solution. Uses the textafter() function.
Referencing Cell C12.
=TEXTAFTER(CELL("filename",C12),"]")
- Gordon_FiremanCopper Contributor
There is one important issue!
If You have only one sheet in Your book, the Formula CELL("filename",C12) will return only the full path with filename, without the name of sheet. So keep in mind this case!