Forum Discussion
how to reference the current sheet name in an function?
- Apr 17, 2023
(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)
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 https://support.microsoft.com/en-gb/office/textafter-function-c8db2546-5b51-416a-9690-c7e6722e90b function can be very useful for this sort of thing.
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.
- SparksnFlashJul 22, 2024Copper Contributor