Excel Reference

Formatting and VBA codes for headers and footers

The following special formatting and Visual Basic for Applications (VBA) codes can be included as a part of the header and footer properties (LeftHeader, CenterHeader, RightHeader, LeftFooter, CenterFooter, and RightFooter).

Format codeDescription
&LLeft aligns the characters that follow.
&CCenters the characters that follow.
&RRight aligns the characters that follow.
&ETurns double-underline printing on or off.
&XTurns superscript printing on or off.
&YTurns subscript printing on or off.
&BTurns bold printing on or off.
&ITurns italic printing on or off.
&UTurns underline printing on or off.
&STurns strikethrough printing on or off.
&”fontname”Prints the characters that follow in the specified font. Be sure to include the double quotation marks.
&nnPrints the characters that follow in the specified font size. Use a two-digit number to specify a size in points.
&colorPrints the characters in the specified color. User supplies a hexadecimal color value.
&”+”Prints the characters that follow in the Heading font of the current theme. Be sure to include the double quotation marks.
&”-“Prints the characters that follow in the Body font of the current theme. Be sure to include the double quotation marks.
&K _xx_. _S_ _nnn_Prints the characters that follow in the specified color from the current theme.

_xx_ is a two-digit number from 1 to 12 that specifies the theme color to use.

_S_ _nnn_ specifies the shade (tint) of that theme color. Specify _S_ as + to produce a lighter shade; specify _S_ as – to produce a darker shade.

_nnn_ is a three-digit whole number that specifies a percentage from 0 to 100.

If the values that specify the theme color or shade are not within the described limits, Excel will use the nearest valid value.
VBA codeDescription
&DPrints the current date.
&TPrints the current time.
&FPrints the name of the document.
&APrints the name of the workbook tab.
&PPrints the page number.
&P+numberPrints the page number plus the specified number.
&P-numberPrints the page number minus the specified number.
&&Prints a single ampersand.
&NPrints the total number of pages in the document.
&ZPrints the file path.
&GInserts an image.

Example

The following code shows how formatting and VBA codes can be used to modify the header information and appearance.

vb Sub Date_Time() ActiveSheet.PageSetup.CenterHeader = "&D &B&ITime:&I&B&T" End Sub

!include[Support and feedback]

3 community Q&A using Formatting and VBA codes for headers and footers

381 words1 sectionsMicrosoft Docs rev. 06/08/2017