Sub topic 8
Adding headers, footers, and page numbers
This chapter assumes you can use the ribbon, select ranges, and already know the difference between a worksheet and a workbook. It also assumes you have set a print area and an orientation, as covered in earlier chapters. Here you will add running text to the top and bottom of every printed page. That text includes page numbers, the file name, the sheet name, and the date.
What headers and footers are
A header is text printed in the top margin of each page. A footer is text printed in the bottom margin. Neither one lives in a worksheet cell. Excel stores them as part of the sheet's page setup, so they never shift your data and they never appear on screen in Normal view.
Each header or footer has three sections: left, center, and right. You can fill any combination of them.
The text sits inside the margins. If a margin is too small, the header or footer can collide with the data. Check the margin settings from the previous chapter if the two overlap.
Adding a header or footer
You can work in two places.
- Go to the Insert tab and choose Header & Footer. Excel switches to Page Layout view and places the cursor in the center header box.
- Or open Page Layout, click the small arrow in the Page Setup group, and use the Header/Footer tab. Choose Custom Header or Custom Footer for the three-section editor.
In Page Layout view, click in any of the three boxes at the top or bottom of a page and type. The Header & Footer tab appears on the ribbon with buttons for page number, file name, and the other items.
The header and footer codes
Each button inserts a code that begins with an ampersand (&). Excel replaces the code with the real value when it prints. You can also type the codes yourself.
| Code | Prints | Button name |
|---|---|---|
| `&P` | Current page number | Page Number |
| `&N` | Total number of pages | Number of Pages |
| `&D` | Current date | Current Date |
| `&T` | Current time | Current Time |
| `&F` | File name | File Name |
| `&A` | Sheet name | Sheet Name |
&F File name
&A Sheet name
&D Current date
&P Page number
&N Total number of pages
Page &P of &NTyping Page &P of &N in the center footer box prints "Page 1 of 4", "Page 2 of 4", and so on. You can mix your own text with the codes. For example, &F - &A in the left header prints something like "Budget.xlsx - Q3".
A worked example
Suppose you have a sales report that spans several pages. You want the file name at the top left, the date at the top right, and a page count at the bottom center.
- Open Insert > Header & Footer.
- Click the left header box and choose File Name. The box shows
&[File]. - Click the right header box and choose Current Date. The box shows
&[Date]. - Scroll to the footer, click the center box, type
Page, choose Page Number, typeof, then choose Number of Pages. - Click a cell in the worksheet to leave the footer editing mode. The footer shows
Page &[Page] of &[Pages].
Switch to Print Preview with Ctrl+P to confirm the result on each page.
Page numbering options
By default, numbering starts at 1. To change it, open the Page Setup dialog and use the Page tab. The First page number box accepts a number, such as 5, if this sheet continues from another document. Leave it as Auto to start at 1.
Page numbers count pages within one sheet. If you print a whole workbook, Excel numbers pages continuously across sheets. When you export sheets separately, each file restarts at 1 unless you set a starting number.
Different first page and odd/even pages
On the Header/Footer tab, two check boxes change how the sections behave.
- Different first page lets the first page carry its own header and footer, for example a title on page 1 and nothing after it.
- Different odd and even pages lets you set one design for odd pages and another for even pages. This suits double-sided printing.
Common problems
- Code prints literally. If you see
&Pon paper, the text was probably entered in a cell and not in the header box. - An ampersand disappears. In a header, a single
&begins a code. To print a real ampersand, type&&. For example,Sales && Marketingprints "Sales & Marketing". - Text is cut off. Long text in one section can overlap the next. Shorten it or move it to another section.
- Footer does not show on screen. Headers and footers appear only in Page Layout view and Print Preview, not in Normal view.
- Changes affect only one sheet. Each sheet has its own settings. Group the sheets first by holding Ctrl and clicking their tabs, and the edit applies to all of them. Ungroup afterward so later edits stay separate.
Exercise
A footer must read "Report.xlsx, Sheet: Summary, Page 2 of 6" on page 2, with the file name, sheet name, and page numbers coming from Excel. What text goes in the center footer box?
Show answer
Type &F, Sheet: &A, Page &P of &N. The code &F supplies the file name, &A the sheet name, &P the current page, and &N the total number of pages.
Exercise
You type Research & Development in the header, and it prints as "Research Development" with the ampersand missing. How do you fix it?
Show answer
Type Research && Development. A single ampersand starts a code, so a literal ampersand must be doubled.