Insert The File Path and Filename into Header or Footer

,

This post will guide you how to add the file path and filename into a header or footer in Excel. How do I insert the file path and filename into the current worksheet header or footer in Excel.

1. Video: Insert The File Path and Filename into Header or Footer

This video demonstrates how to insert the file path and filename into the header or footer of an Excel file using VBA code, as well as through manual steps.

2. Insert The File Path and Filename into Header or Footer

If you want to put a file path and filename into your worksheet header or footer in Excel, you just need to use the Header and Footer tools to add it. Do the following steps:

Step1: go to INSERT tab, and click Header& Footer command under Text group.

insert file path and filename header footer1

Step2: click header in your worksheet, and go to DESIGN tab, click File Path and File Name commands under Header& Footer Elements group.

insert file path and filename header footer2

Step3: click any cell in your worksheet, and the file path and file name will be inserted into the selected header in your worksheet.

Insert The File Path and Filename into Header or Footer 13.png

Note: The file path and filename will be updated automatically if the file is saved with a new name or in a different location.

3. Insert The File Path and Filename into Header or Footer with VBA Code

You can also use VBA code to insert the file path and filename into the header or footer in Excel. Just do the following steps:

Step1: open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.

Get the position of the nth using excel vba1

Step2: then the “Visual Basic Editor” window will appear.

Step3: click “Insert” ->”Module” to create a new module

convert column number to letter3

Step4: paste the below VBA code into the code window. Then clicking “Save” button.

Insert The File Path and Filename into Header or Footer 10.png
Sub InsertFilePathAndFileNameInHeader_excelhow()
    Dim Path As String
    Dim Filename As String
    Dim Header As Variant
    Path = ThisWorkbook.Path
    Filename = ThisWorkbook.Name
    With ActiveSheet
        Set Header = .PageSetup
        Header.CenterHeader = "File Path: " & Path & Chr(10) & "File Name: " & Filename
    End With
End Sub

Step5: back to the current worksheet, then run the above excel macro. Click Run button.

Insert The File Path and Filename into Header or Footer 11.png

Step6: The file path and filename will be inserted into the header or footer of the active sheet.

Insert The File Path and Filename into Header or Footer 12.png

The above VBA macro will insert the file path and the file name into the center header. and if you want to insert it to other position, you can specify a different place for the path and filename, you just need to change CenterHeader  to another location, such as: CenterFooter, LeftFooter, RightFooter, Leftheader, or RightHeader.

4. Conclusion

Adding the file path and filename into the header or footer can be useful for tracking and identifying your spreadsheet files. While there are manual steps to insert this information, you can also use VBA code to automate the process. By using VBA code, you can easily insert the file path and filename into the header or footer of your Excel files, saving you time and effort.

Leave a Reply