Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Adding a File Path and Filename.

Adding a File Path and Filename

Written by Allen Wyatt (last updated May 25, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Margo wants to insert a file path and filename in an Excel worksheet. She wants to be able to insert the information in either a cell or into the header/footer. This is rather easy to do in Excel.

To insert the file path and filename into a cell, you use the CELL worksheet function in the following manner:

=CELL("filename")

This formula returns the entire path, filename, and tab name of the current worksheet, like this:

E:\My Data\Excel\[Budget.xlsx]Sheet1

If you want to strip out the brackets and the sheet name, you can use the following variation on the formula:

=SUBSTITUTE(LEFT(CELL("filename"),FIND("]",CELL("filename"))-1),"[","")

The LEFT function gets rid of everything from the right bracket to the end of the string, while the SUBSTITUTE function gets rid of the left bracket.

If you are using Excel 2021 or the version of Excel in Microsoft 365, you could also use this formula to get the stripped-down workbook name:

=TEXTAFTER(TEXTBEFORE(CELL("filename"),"]"),"[")

Putting a path and filename into a header or footer is easy:

  1. Select the worksheet whose header or footer you want to change.
  2. Display the Page Layout tab of the ribbon.
  3. Click the small icon at the bottom-right of the Page Setup group. Excel displays the Page Setup dialog box.
  4. Make sure the Header/Footer tab is displayed.
  5. Click on either the Custom Header or Custom Footer buttons, as desired. Word displays either the Header or Footer dialog box. (See Figure 1.)
  6. Figure 1. The Header dialog box.

  7. Position the insertion point in the Left Section, Center Section, or Right Section boxes, as desired.
  8. Click on the Insert File Path button. (It looks like a file folder with a piece of paper sticking out.) Excel inserts the following code at the insertion point:
  9.      &[Path]&[File]
    
  10. Click on OK two times to close both dialog boxes.

When you print the worksheet, Excel replaces the codes in step 5 with the path name and the file name of the workbook, respectively.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11356) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Adding a File Path and Filename.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing a Font List

Getting a list of fonts available in a document is not something you can easily do in Word. That is, unless you put the ...

Discover More

Highlighting Text Using the Keyboard Only

Highlighting text, using the Highlight tool, is a great way to mark up a document. Normally you need to use the toolbar ...

Discover More

Style Names Can Affect Style Definitions

Most people think that you can name styles almost anything you want. You can, but there may be some unintended ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Importing CUSIP Numbers Correctly

It is often necessary to import information from other programs into Excel. Sometimes this can lead to challenges, such ...

Discover More

Importing Huge Data Files

Sometimes, when importing data created by other programs, you may find that there is too much for Excel to handle. Here's ...

Discover More

Full Path to Workbook

Need to get the full path of your current workbook into the Clipboard? Excel doesn't make it quite as easy as it used to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is eight less than 8?

2024-05-29 11:18:31

J. Woolley

The Tip includes this formula to return the workbook's name (minus path and sheet)
    =TEXTAFTER(TEXTBEFORE(CELL("filename"), "]"), "[")
but it requires Excel 2021+.
My Excel Toolbox includes the following function to return the substring of Text between two substrings BeginAfter and EndBefore:
    =Between(Text, BeginAfter, EndBefore, [CaseSensitive], [Direction])
If Direction is -1, the search is right-to-left for both BeginAfter and EndBefore; therefore, this formula returns the workbook's name
    =Between(CELL("filename"), "[", "]", , -1)
and this formula returns the workbook's parent folder
    =Between(CELL("filename"), "\", "\[", , -1)
and this formula returns the workbook's path
    =Between(CELL("filename"), "", "\[", , -1)
Since the three previous formulas search right-to-left, they compensate for a path that might have [ and/or ] brackets in a folder's name; the Tip's formulas do not.
My Excel Toolbox includes two more functions that are useful for this subject:
    =FindRev(FindText, WithinText, [StartNum], [CaseSensitive])
    =NameOf([This], [Target])
Unlike Excel's FIND function, FindRev searches in reverse (right-to-left) to return the location of FindText in WithinText.
NameOf is described in the following two references:
My 2022-04-06 comment: https://excelribbon.tips.net/T007850
My Jul 19, 2019 article: https://wellsr.com/vba/2019/excel/vba-udf-to-enhance-excel-cell-and-info-functions/
NameOf("book") returns the formula cell's workbook name; NameOf("path") returns its path.
Notice CELL("filename") does not always update when switching between two open workbooks. (Press F9 to update.) The NameOf function is Volatile, so that is not a problem.
See https://sites.google.com/view/MyExcelToolbox


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.