How to Format Existing Excel Page Header Text with VBA Without Deleting It
Question details
The user wants to apply new formatting (such as font size or weight) to an existing Excel page header using VBA without overwriting or deleting the text that is already present.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Applying VBA formatting codes to worksheets where the headers already contain custom text.
- Observed behavior
- Assigning a new formatting value directly to the PageSetup.CenterHeader property replaces the entire existing header, resulting in the loss of the original text.
Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled the Developer tab to access the VBA editor.
Use VBA Concatenation to Append Formatting Codes
Preserve the existing header text by concatenating your desired formatting codes to the current header value.
When you assign a new string to a header property, Excel completely overwrites the previous contents. By concatenating formatting codes with the existing property value, you can apply new styles while keeping the original text intact.
Be aware that if the existing header already contains multiple intermingled formatting codes, adding new ones might cause complex overlaps. Strip or adjust existing formats manually if necessary.
Open your Excel workbook and press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) Editor.
Click 'Insert' from the top menu bar and select 'Module' to create a new blank script window.
Copy and paste the following code into the module: Sub FormatHeader() With ActiveSheet.PageSetup .CenterHeader = "&""Arial,bold""&10" & .CenterHeader End With End Sub
Ensure that you are using standard straight quotation marks in your code, as curly quotation marks (smart quotes) will cause VBA syntax errors.
Press F5 or click the 'Run' button in the toolbar to execute the macro and apply the formatting to the active sheet's header.

Format Headers with Macros in WPS Spreadsheet
WPS Spreadsheet offers strong compatibility with Microsoft Excel VBA macros. You can seamlessly run your page setup and formatting scripts using the WPS Macro editor without altering your code.
- 1. Enable the Developer Tab: Open your document in WPS Spreadsheet, navigate to the top ribbon, and click on the 'Developer' tab.
- 2. Launch the VBA Editor: Click on the 'Macros' or 'Visual Basic' icon to open the integrated VBA editor.
- 3. Run Your Formatting Script: Paste your page setup VBA script into a new module and press F5 to execute your header formatting macro instantly.

Frequently Asked Questions
How do I strip existing formatting codes from an Excel header using VBA?
Stripping existing formatting automatically via VBA is complex because formatting codes and plain text are often intermingled. You typically need to use string manipulation functions like 'Replace' to manually identify and remove specific ampersand (&) formatting markers from the .CenterHeader string.
Why did my header text disappear when I ran my VBA script?
This happens if you assign only the formatting string to .CenterHeader (e.g., .CenterHeader = "&14"). To retain the existing text, you must concatenate the formatting code with the current header property (e.g., .CenterHeader = "&14" & .CenterHeader).
What is the VBA code to change the header font size only?
You can change the font size by using an ampersand followed by the desired size number. For example, to change the size to 14 without deleting text, use the code: .CenterHeader = "&14" & .CenterHeader.
Can I format the left or right header sections the same way?
Yes, the identical concatenation method applies. Simply replace .CenterHeader with .LeftHeader or .RightHeader in your VBA code to target and format those specific sections.




