Skip to main content

How to protect header and footer in Excel?

In Excel, we can protect the cells quickly and easily, but, have you ever tried to protect the header and footer to prevent others modifying them? Normally, there is no direct way to solve this task in worksheet, but, the following VBA code may do you a favor.

Protect header and footer with VBA code


arrow blue right bubble Protect header and footer with VBA code

Header and footer are useful for us when printing worksheet, to prevent them being changed by others, please do with following steps:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Then double click ThisWorkbook under the VBAProject section to open a blank module, and then copy and paste the following code into the module:

VBA code: Protect header and footer

Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Call ProtectHF
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Call ProtectHF
End Sub
Private Sub Workbook_Open()
    Call ProtectHF
End Sub
Private Sub ProtectHF()
'Updateby Extendoffice
    Application.ScreenUpdating = False
    ThisWorkbook.ActiveSheet.PageSetup.RightHeader = ""
    ThisWorkbook.ActiveSheet.PageSetup.RightFooter = ""
    ThisWorkbook.ActiveSheet.PageSetup.CenterFooter = "KTE-Sale-Report"
    ThisWorkbook.ActiveSheet.PageSetup.CenterHeader = "Created by Skyyang"
    ThisWorkbook.ActiveSheet.PageSetup.LeftHeader = ""
    ThisWorkbook.ActiveSheet.PageSetup.LeftFooter = ""
    Application.ScreenUpdating = True
End Sub

doc protect header footer 1

Note: You can insert the header or footer content into the corresponding location in the above code.

3. And then save and close this code, now, while clicking Save button to save this workbook, the modified header or footer content will be return to the original ones automatically if you have tried to change the header or footer , see screenshot:

doc protect header footer 2

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
J'aime beaucoup, mais ce n'Est qu'une semi-sécurité pour l'en-tête. Je peux modifier les en-tête et les pied de page et imprimer avec la modification. Comment je pourrais empêcher toute modification exactement comme avec la protection de la feuille ???
This comment was minimized by the moderator on the site
Hello, Michael,
In fact, when you print the worksheet, the header or footer are not changed. It just changes the display in the Print Preview.
Please try it again, thank you!
This comment was minimized by the moderator on the site
Posso inserir uma imagem no cabeçalho?

Porque o meu cabeçalho é o logotipo da empresa.

Obrigada!
This comment was minimized by the moderator on the site
After shutting down and reopening the excel file, the header/footer can still be deleted. So how to fix this? 
Thank you very much!
This comment was minimized by the moderator on the site
Hello, Tracy,When you close the workbook, you should save it as Excel Macro-Enabled Workbook format, this will save the code.Reopening this file next time, please click Enable Content button from the top of the formula bar to activate the code.Please try, hope it can help you!
This comment was minimized by the moderator on the site
notas: Puede insertar el contenido del encabezado o pie de página en la ubicación correspondiente en el código anterior.
No entiendo ese paso , por favor si pueden explicarme, lo agradecería.
This comment was minimized by the moderator on the site
Hi used this with auto sheet name, file location, and page number and when saving it requires you to click in each field to print correctly.
This comment was minimized by the moderator on the site
Hi, Love the code to protect the header and footer, is there a way to change the font size, style, and color in the code so it is always replaced with a larger font?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations