If you want to insert certain information in the header / footer of the worksheet like the file name / file path or the current date or page number, you can do so using the below code. If it is just one worksheet you can do it manually, but if it is multiple sheets or all sheets in the workbook which need this information to populated, you can do this using a simple vba macro / code.
This sample macro will insert a header/footer in every worksheet in the active workbook. It will also insert the complete path to the workbook.
Option Explicit Sub InsertHeaderFooter() Dim wsAs Worksheet Application.ScreenUpdating = False Each wsInThisWorkbook.Worksheets With ws.PageSetup .LeftHeader = “Company Name:” .CenterHeader = “Page &P of &N” .RightHeader = “Printed &D &T” .LeftFooter = “Path : “ &ActiveWorkbook.Path .CenterFooter = “Workbook Name: & F” .RightFooter = “Sheet: &A” End With Next ws Set ws = Nothing Application.ScreenUpdating = True End Sub
To copy this code to your workbook, press Alt + F11 on your keyboard. Then on the left hand side, you will see Microsoft Excel Objects. Right click and select Insert. Then click on Module and copy this code to the code window on the right.
Lets break up each part of the code –
We start with the usual Dim statement where we declare the variables. In this case, we have only 1 variable – ws for the worksheet. Then we disable screen updating.
Now, in the FOR loop, we loop through each worksheet in the workbook which contains the macro. And we setup each parameter in Page Setup. &P, &N, &D, &T, &F and &A are certain format codes which can be applied to headers & footers. &P prints the page number. &N prints the total number of pages in the document. &D prints the current date. &T prints the current time. &F prints the name of the document and &A prints the name of the workbook tab.
At the end we set the worksheet to nothing and free the object and enable screen updating.
Here are 2 pictures. The 1st one shows you the header and the 2nd one the footer after the macro has been run.
The header has the label Company Name. The name is not entered in yet since we haven’t linked it to any cell or fed in any text for the Company Name. If you enter anything in the code or in the excel sheet and reference it, then the name will be picked up and populated here.
Page # of 0 shows that currently we have 0 pages in the file, since we have run this code on a blank file. If you run this code on a file containing data, it will show you the page number.
Printed <Date><Time> gives you the date and time the macro was run along with the text “Printed”.
In the Footer, the Path label will show you the path of the current file.
Our filename is Book1.xlsx which is currently an unsaved file. Hence there is no path showing up for the Path label.
The Sheet number is populated to the right of the footer.
If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.
We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at email@example.com
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.