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.
Table of Contents
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.
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.
Step2: click header in your worksheet, and go to DESIGN tab, click File Path and File Name commands under Header& Footer Elements group.
Step3: click any cell in your worksheet, and the file path and file name will be inserted into the selected header in your worksheet.
Note: The file path and filename will be updated automatically if the file is saved with a new name or in a different location.
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.
Step2: then the “Visual Basic Editor” window will appear.
Step3: click “Insert” ->”Module” to create a new module
Step4: paste the below VBA code into the code window. Then clicking “Save” button.
Dim Path As String
Dim Filename As String
Dim Header As Variant
Path = ThisWorkbook.Path
Filename = ThisWorkbook.Name
Set Header = .PageSetup
Header.CenterHeader = "File Path: " & Path & Chr(10) & "File Name: " & Filename
Step5: back to the current worksheet, then run the above excel macro. Click Run button.
Step6: The file path and filename will be inserted into the header or footer of the active sheet.
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.
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.