Sub topic 8
Adding headers, footers, and page numbers
This chapter assumes you can open the Page Setup dialog, you know the difference between Normal view and Page Layout view, and you have already set a print area and orientation. Headers and footers are the text that Excel prints in the top and bottom margins of every page. They do not occupy worksheet cells, so they never shift your data or change where pages break.
How headers and footers work
Each page has a header band in the top margin and a footer band in the bottom margin. Each band is divided into three sections: left, center, and right. You can put text, a code, or a picture in any section.
Headers and footers belong to the sheet, not to the workbook. If your workbook has four sheets, you must set up each one, or select the sheets as a group before you edit (covered below).
The header and footer bands sit inside the margins. Their distance from the paper edge is controlled by the Header and Footer margin values on the Margins tab of Page Setup. If the header margin is larger than the top margin allows, the header will collide with your data. The usual rule is to keep the top margin larger than the header margin by at least the height of one line of text.
Three ways to edit them
You can reach the same settings from three places. Choose by how much control you need.
| Method | How to open it | Best for |
|---|---|---|
| Page Layout view | View tab, then Page Layout. Click the "Click to add header" area at the top of a page. | Seeing the result in place as you type |
| Page Setup dialog | Page Layout tab, then the dialog launcher in the Page Setup group. Select the Header/Footer tab. | Choosing built-in presets and using Custom Header or Custom Footer |
| Insert tab | Insert tab, then Text group, then Header & Footer | A shortcut that switches to Page Layout view |
In Page Layout view, clicking a header section activates the Header & Footer contextual tab on the ribbon. Its Header & Footer Elements group has buttons for page number, number of pages, current date, current time, file path, file name, sheet name, and picture. Each button inserts a code (explained next) at the cursor position.
Header and footer codes
When you click a button such as Page Number, Excel does not insert a number. It inserts a code that begins with an ampersand (&). At print time, Excel replaces the code with the real value for that page. You can type these codes yourself in a custom header or footer box.
&F File name
&A Sheet name
&D Current date
&P Page number
&N Total number of pages
Page &P of &NOther codes you will meet:
&Tinserts the current time.&Zinserts the file path without the file name.&Gmarks where a picture goes.&&prints a literal ampersand. This matters if your text contains "R&D" or "Q&A". TypeR&&Dto get "R&D".&Btoggles bold,&Itoggles italic, and&Utoggles underline for the text that follows.&"Font name,Style"and&12(a number) change the font and size for the text that follows.
A common error: typing a number directly after a code. In &P1, Excel can read the digits as a font size change. If you need a number right after a code, put a space between them, for example &P 1.
The date and time codes use the computer's clock at the moment of printing, not the date the file was saved. A report printed on Monday shows Monday's date even if you last edited it in March. That is usually what you want for a "Printed on" stamp. If you need the date of the data itself, put it in a cell instead.
Worked example: a "Page X of Y" footer
Follow these steps on any sheet with enough rows to run onto at least two pages.
- Open the Page Setup dialog and select the Header/Footer tab.
- Click Custom Footer.
- Click in the Center section box.
- Type
Page, then click the Insert Page Number button (the first icon with a "#"). Typeof, then click Insert Number of Pages (the second icon). - The box now reads
Page &[Page] of &[Pages]. The&[Page]and&[Pages]tokens are how the dialog displays&Pand&N. - Click in the Left section box and click Insert File Name.
- Click in the Right section box and click Insert Date.
- Click OK, then open Print Preview (Ctrl+P) to check the result.
The footer on page 2 of a 5-page report now reads like this: the file name on the left, "Page 2 of 5" in the center, and the print date on the right.
Built-in presets and their limits
The Header and Footer drop-down lists in Page Setup offer presets such as "Page 1 of ?", the sheet name, the user's name, and combinations separated by commas. Presets are fast, but they have two limits:
- They draw their values from the file, such as the author name from document properties. A preset that shows "Prepared by Alex" shows whatever name is stored in the file, which may be out of date or belong to a colleague.
- They place fixed items in fixed sections. If you want the sheet name on the left and the page number on the right, build it as a custom header or footer.
Starting at a different page number
By default, the first page is numbered 1. To change this, open the Page tab of Page Setup and type a value in First page number. This is useful when you print a section of a longer document and need the numbers to continue from the previous section. Leave it as Auto for normal behavior.
Note that the number applies to the first page of each print job. If you print three sheets in one job, numbering runs continuously across them only when they are printed together (the whole workbook or a group of selected sheets). Printing the same three sheets as three separate jobs restarts the count each time.
Different first page and different odd and even pages
On the Header/Footer tab, two check boxes change which header and footer appear where:
- Different first page gives page 1 its own header and footer. A typical use is a title page with no page number, while later pages show "Page &P of &N".
- Different odd and even pages lets you mirror the layout for double-sided printing. For example, put the page number at the right on odd pages and at the left on even pages, so it always sits at the outer edge.
When you turn either option on, the Custom Header and Custom Footer dialogs gain extra tabs or sections for each variant. If a page shows a blank header, check whether you set the "first page" variant but left it empty.
Two more options sit beside them:
- Scale with document makes header and footer text shrink or grow together with the worksheet's scaling percentage. If you shrink a sheet to fit one page wide, a header with this option on shrinks too, and may become hard to read.
- Align with page margins keeps the left and right header sections lined up with the left and right page margins rather than with the edge of the paper. Leave it on for tidy alignment with your data.
Adding a picture, such as a logo
In Page Layout view, click a header section, then click Picture in the Header & Footer Elements group. Select an image file. Excel inserts the code &[Picture] (stored as &G). To resize or crop it, click the Format Picture button that appears in the same group.
Keep the logo small. A tall image forces the header band to grow, and it pushes the body of the page down or overlaps it if the top margin is too small. Raise the top and header margins if the logo is clipped.
Setting headers and footers on several sheets at once
To give many sheets the same header and footer, group them first. Click the first sheet tab, hold Ctrl and click the others (or hold Shift and click the last tab to select a range). The title bar shows "[Group]". Now any change in Page Setup applies to every grouped sheet.
Page Setup changes made in a group overwrite the existing header and footer on each sheet, along with any other settings you change in the same dialog. Review each sheet in Print Preview afterward.
Applying the same codes with VBA
The same codes work in VBA through the PageSetup object. The properties are LeftHeader, CenterHeader, RightHeader, LeftFooter, CenterFooter, and RightFooter. The macro below uses the footer code you built by hand.
Sub AddStandardFooter()
With ActiveSheet.PageSetup
.LeftFooter = "&F"
.CenterFooter = "Page &P of &N"
.RightFooter = "&D"
.CenterHeader = "&""Calibri,Bold""&14&A"
End With
End SubIn VBA a double quote inside a string is written as two double quotes. The header line above produces &"Calibri,Bold"&14&A: bold Calibri at 14 points, followed by the sheet name. Setting PageSetup properties is slow on some systems because Excel talks to the printer driver, so apply them once per sheet instead of inside a loop over many cells.
Common problems
- Header or footer does not appear in the printout. Check that the header margin is not larger than the top margin, and that the text is not set in white or at a very small size by an
&size code. Also check that you edited the intended sheet. - Page numbers show as "&P" or "&[Page]" literally. The code was typed in a worksheet cell instead of in the header or footer box. Codes only work in header and footer boxes.
- "Page 1 of 1" on every page of a multi-sheet print. The sheets were printed as separate jobs. Select all the sheets you want, then print once.
- Footer text is clipped at the bottom. Raise the bottom margin, or lower the footer margin value.
- Ampersand disappears from text. Double it:
&&. - Header text is tiny after fitting to one page wide. Turn off Scale with document.
Summary
Headers and footers live in the margins and consist of left, center, and right sections. Codes beginning with & insert values such as the page number, total pages, file name, sheet name, and date at print time. You can edit them in Page Layout view or in the Page Setup dialog, vary them for the first page or for odd and even pages, and apply them to many sheets by grouping. The next chapter builds on this by looking at which sheet elements, such as gridlines and comments, print.
Exercise
A report prints as 6 pages. You want page 1 to have no page number, and pages 2 to 6 to show "Page 2 of 6" and so on in the center of the footer. Which setting and which footer text do you use, and what is one thing to check on page 1?
Show answer
On the Header/Footer tab of Page Setup, turn on "Different first page". Leave the first-page footer empty. In the regular Custom Footer, put Page &P of &N in the center section. On page 1, check that the first-page footer is truly empty, and note that page 1 still counts as page 1, so the next page shows "Page 2 of 6". If you want numbering to start at 1 on the second page, you would need to change the First page number setting instead.
Exercise
Your footer should read "Sales & Marketing" on the left. After printing, it reads "Sales Marketing" with the ampersand missing. What is wrong, and what do you type to fix it?
Show answer
Excel treats a single ampersand as the start of a code, so it consumed the character. Type Sales && Marketing in the left footer section so that the double ampersand prints as one literal ampersand.