How to Correct VBA Code for Excel Chart Sheets and Axes
Question details
Users need to correct or rewrite failing VBA code generated by the Macro Recorder when formatting Excel chart sheets, shapes, and axes.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Editing or reusing recorded VBA macros to automate the formatting of chart areas and axes.
- Observed behavior
- VBA code created by the Macro Recorder for chart sheets fails upon reuse because it rigidly references specific recorded object IDs or text frames rather than generic, reusable chart properties.
Ensure you have access to the Visual Basic Editor (VBE) and consider creating a backup copy of your workbook before editing or running new macro code.
Use Direct Chart Area and Axis Formatting Code
Replace unreliable Macro Recorder code with direct object references to format the chart area and adjust axis properties reliably.
The Macro Recorder often generates highly specific code tied to unique object IDs that may not exist when the macro is run again. Writing direct VBA using ActiveChart properties ensures your code applies cleanly to any currently selected chart without referencing temporary shapes.
Press ALT + F11 on your keyboard to open the Visual Basic Editor (VBE), then locate the module containing your recorded chart macro.
Delete the recorded shape references and use the `With ActiveChart.ChartArea` block to apply colors directly. For example: `With ActiveChart.ChartArea .Interior.Color = RGB(192, 192, 192) .Border.Color = RGB(0, 0, 128) End With`.
To change the font size for the value axis, use this direct command: `ActiveChart.Axes(xlValue).TickLabels.Font.Size = 8`.
Select a chart in your spreadsheet and run the macro to verify that the formatting applies correctly without generating object reference errors.

Create and Edit Advanced Macros in WPS Spreadsheets
WPS Spreadsheets provides comprehensive support for VBA macros, allowing you to seamlessly edit, debug, and run custom chart formatting code directly within a highly compatible environment.
- 1. Open WPS Spreadsheets: Launch WPS Office and open your macro-enabled spreadsheet file.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'Visual Basic Editor' (or press ALT + F11).
- 3. Edit Your Chart Macro: Insert a new module or edit your existing macro with the direct `ActiveChart` VBA code provided in the solution.
- 4. Run and Save: Run your VBA code to format your charts automatically, and save your document as a macro-enabled workbook (.xlsm).

Frequently Asked Questions
Why does my recorded VBA code fail when I run it a second time?
The Macro Recorder captures exact user actions, including clicks on specific object IDs (like 'Chart 1' or 'Text Box 2'). When run again, those exact objects might not be selected or may no longer exist, causing a runtime error. Using dynamic references like `ActiveChart` solves this.
How do I change the font size of the category axis using VBA?
You can reference the category axis similarly to the value axis by using the `xlCategory` parameter. The syntax is: `ActiveChart.Axes(xlCategory).TickLabels.Font.Size = [Your Target Size]`.
Where can I find the correct VBA syntax for different chart elements?
The most reliable method is to use the Object Browser inside the Visual Basic Editor. Press F2, search for 'Chart' or 'Axis', and you will see a complete list of valid properties, methods, and collections available for scripting.




