SOLVED

how to reference the current sheet name in an function?

Copper Contributor

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!

14 Replies

@Needsleep 

You can use the CELL function to reference the current sheet name in an Excel function.

Here’s how:

  1. Click on the cell where you want to reference the current sheet name.
  2. Type =CELL("filename") in the formula bar and press Enter.
  3. 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.
  4. 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).
  5. 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! 

best response confirmed by Needsleep (Copper Contributor)
Solution

@Needsleep 

 

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

 

@Needsleep 

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

I did not underetand.

@NikolinoDE 

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".

@RickSeiden 

That's why it shall be used with reference like in @L z. post.

@Sergei Baklan I changed my formula to

 

=CELL("filename",A1)

 

as shown in the other post you mention, and I see the same behavior.  But I tried the version with RIGHT and LEN instead of the TEXTAFTER version, and that works as expected.

 

Thanks for pointing out my mistake!

@Needsleep 

 

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.

 

 

@RichHolton 

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 @L z. above.

The 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 @L z. 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.
Thanks for the explanation on this @RichHolton!

@L z. 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.

@Needsleep =(MID(@CELL("filename",A1),FIND("]",@CELL("filename",A1))+1,256)) try this one; it worked for me

This is the one!! Thanks
1 best response

Accepted Solutions
best response confirmed by Needsleep (Copper Contributor)
Solution

@Needsleep 

 

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

 

View solution in original post