VBA & Macro Problems

Format Existing Excel Worksheet Header Text with VBA

Khadija KhanKhadija Khan Nov 22, 2026 868 views

Question details

Format text in an Excel worksheet header with VBA while handling the embedded header formatting codes correctly.

Product
Microsoft Excel
Device & OS
not provided
Scenario
The user wants to update or reformat existing left header text, including applying Segoe UI, 12-point, bold formatting.
Observed behavior
Reading PageSetup.LeftHeader returns the header text together with codes for formatting such as font, style, and size.
Before you start

Open the workbook in Excel and identify the worksheet and header section you want to change. In the VBA editor, open the Immediate window from View > Immediate Window so you can inspect header strings.

Solution 1Recommended

Create a formatted header template and use it in VBA

Use this method to apply a consistent font and size without manually constructing every header formatting code.

Excel stores header formatting codes and text together in the PageSetup.LeftHeader string. Creating a sample header lets Excel generate the codes for you; it does not automatically extract plain text from an existing formatted header.

1
Create a sample header

In a new worksheet, enter a value in cell A1. Go to Insert > Header & Footer, click the left header section, type AAA, and format it as Segoe UI, 12-point, bold using the font controls.

2
Inspect the generated string

Open the VBA editor and choose View > Immediate Window. Enter ?ActiveSheet.PageSetup.LeftHeader and press Enter to display the header string.

3
Make a VBA string literal

Copy the displayed string into a text editor, double every quotation mark, and surround the whole string with quotation marks. For example, &"Segoe UI,Bold"&12AAA becomes "&""Segoe UI,Bold""&12AAA" in VBA.

4
Replace the placeholder and assign the header

In a VBA module, use the captured string as a template. For example: Sub SetLeftHeader() Const HeaderTemplate As String = "&""Segoe UI,Bold""&12AAA" Dim HeaderText As String HeaderText = "Table 00Test Table Title" ActiveSheet.PageSetup.LeftHeader = Replace(HeaderTemplate, "AAA", Replace(HeaderText, "&", "&&")) End Sub. Replace the example template with the exact string Excel generated and set HeaderText to the desired text.

5
Check the result

Run SetLeftHeader from the VBA editor, then open File > Print to preview the worksheet header and verify its text and formatting.

Existing header codes: If the existing header contains multiple formatting runs or dynamic fields, inspect it before copying its visible text into HeaderText. The template replaces the entire left header; it does not preserve those existing runs or fields.
Free Microsoft Office alternative

Edit Spreadsheet Headers with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with spreadsheets and page headers. Its Microsoft Office format compatibility helps you move existing files into WPS, although it does not fix Excel-specific VBA behavior.

Work with spreadsheet page headers in a familiar interfaceOpen and edit Microsoft Excel formats, including XLSXUse a free, lightweight office suiteMove existing Office documents into a familiar workspace
microsoft office alternative - wps office

Frequently Asked Questions

Why does PageSetup.LeftHeader include font codes?

Excel stores the header's text and formatting instructions together in the LeftHeader string. Use the VBA Immediate window to print the property and inspect the complete value.

Can I remove the formatting codes with a simple Replace command?

A simple replacement is unreliable when the header contains several fonts, sizes, or dynamic fields. For a consistent new format, create a sample header template and insert the intended plain text into its placeholder.

How do I include an ampersand in literal header text?

Use && for a literal ampersand in an Excel header string. In VBA, Replace(HeaderText, "&", "&&") prepares plain text before inserting it into a header template.

How can I format the center or right header instead?

Use PageSetup.CenterHeader or PageSetup.RightHeader in place of PageSetup.LeftHeader. Create and inspect a sample in the corresponding header section before assigning the template.

How do I verify the header before printing?

After running the macro, open File > Print in Excel and check the print preview for the correct text, font, size, and placement.