Automatically Add Header Footer to Excel Document with VBA


How can we add a header or footer to an Excel document automatically with VBA. Let’s first find all the built-in properties of an Excel document that can be accessed using a ‘for next’ loop as described in this article ‘Workbook.BuiltinDocumentProperties Property (Excel)‘:

Sub inbuiltDocumentPropertiesList()
rw = 2
Worksheets(2).Activate
For Each p In ActiveWorkbook.BuiltinDocumentProperties
Cells(rw, 1).Value = p.Name
rw = rw + 1
Next
End Sub
We can now add a header or footer or any other built-in property into an Excel worksheet using the following VBA code:
Sub addHeaderFooter()
ActiveSheet.PageSetup.LeftFooter = _
ActiveWorkbook.BuiltinDocumentProperties(“Author”)
ActiveSheet.PageSetup.CenterFooter = _
ActiveWorkbook.BuiltinDocumentProperties(“Creation Date”)
ActiveSheet.PageSetup.CenterHeader = _
ActiveWorkbook.BuiltinDocumentProperties(“Author”)
End Sub

The above code will display the name of the author on the left in the footer. The date of creation of the file will be displayed in the center of the footer. The header will also display the author’s name in the center of the header area. You can also display other built-in properties like ‘Keywords’. The built-in properties can also be displayed in the worksheet cells by modifying the VBA code appropriately.

Watch the video below:


Download a sample file by clicking on the Excel icon:


Further reading:
Workbook.BuiltinDocumentProperties Property

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

One thought on “Automatically Add Header Footer to Excel Document with VBA”

Leave a Reply

Your email address will not be published. Required fields are marked *