Format Existing Excel Worksheet Header Text with VBA
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.
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.
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.
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.
Open the VBA editor and choose View > Immediate Window. Enter ?ActiveSheet.PageSetup.LeftHeader and press Enter to display the header string.
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.
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.
Run SetLeftHeader from the VBA editor, then open File > Print to preview the worksheet header and verify its text and formatting.
Replace known text while preserving existing header codes
Use this approach when the header already has the formatting you want and you only need to change a known text segment.
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.

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.




