Insert the current Excel file name, path, or worksheet in a cell

Let's say you want to add information to a spreadsheet report that confirms the location of a workbook and worksheet so you can quickly track and identify it. There are several ways you can do this task.

What do you want to do?


Insert the current file name, its full path, and the name of the active worksheet

Type or paste the following formula, which uses the CELL function and the “filename” argument (“filename” must be enclosed in quotation marks), in the cell in which you want to display the current file name with its full path and the name of the current worksheet:

=CELL("filename")

 Note    The formula returns the name of the worksheet as long as the worksheet has been saved at least once. If you use this formula on an unsaved worksheet, the formula cell will remain blank until you save the worksheet.

Top of Page Top of Page

Insert the current file name and the name of the active worksheet

The following example uses a combination of functions in an array formula to extract only the file name and the name of the active worksheet from the filename information that is returned by the CELL function.

Type or paste this formula in a cell to display the current file name and active worksheet name and be sure to press CTRL+SHIFT+ENTER to enter it as an array formula:

=RIGHT(CELL("filename"),LEN(CELL("filename"))- MAX(IF(NOT(ISERR(SEARCH("\",CELL("filename"), ROW(1:255)))),SEARCH("\",CELL("filename"),ROW(1:255)))))

 Notes 

  • In order for this formula to return the information as expected, you must enter it as an array formula by pressing CTRL+SHIFT+ENTER. Otherwise, it won’t work exactly as described above.
  • The formula returns the name of the worksheet as long as the worksheet has been saved at least once. If you use this formula on an unsaved worksheet, the formula cell will remain blank until you save the worksheet.

Top of Page Top of Page

Insert the current file name only

The following example expands on the previous example to extract the file name, which is enclosed in left and right bracket characters. It uses the SEARCH function in addition to the MID and CELL functions.

Type or paste the following formula in a cell to display the name of the current workbook:

=MID(CELL("filename"),SEARCH("[",CELL("filename"))+1, SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))-1)

 Note   If you use this formula on an unsaved worksheet, Excel displays the #VALUE! error. After you save the worksheet, the error is replaced by the file name.

Top of Page Top of Page

 
 
Applies to:
Excel 2010