How to Fix Misaligned Excel PivotChart Gridlines with Monthly Grouping
Question details
The user needs to align vertical gridlines with monthly boundaries in an Excel PivotChart when the data is grouped by month and year.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Creating an Excel PivotChart where the timeline data is grouped by both month and year for a reporting dashboard.
- Observed behavior
- Excel's standard interval settings shift the gridlines incorrectly, causing axis divisions and vertical gridlines to misalign with the actual monthly boundaries.
Since this workaround requires VBA macros, ensure you have the Developer tab enabled in Excel and are prepared to save your file as a Macro-Enabled Workbook (.xlsm).
Use a VBA Macro to Draw Custom Vertical Lines
Because Excel does not natively support precise gridline layouts for multilevel category axes, running a custom VBA script to draw shapes directly onto the chart is the most effective solution.
Excel struggles to distribute gridlines correctly across a multilevel category axis (like Month and Year grouped together). By bypassing the built-in gridlines, you can instruct Excel via VBA to draw perfectly aligned vertical lines at specific data points.
Select your PivotChart, then press 'Alt + F11' on your keyboard to open the Microsoft Visual Basic for Applications window.
In the VBA editor, click 'Insert' from the top menu, then select 'Module' to create a blank script window.
Paste your custom VBA macro code designed to calculate the axis coordinates and draw vertical line shapes at the required monthly intervals.
Close the VBA editor, ensure your chart is still selected, and press 'Alt + F8'. Select your macro from the list and click 'Run'.

Try WPS Spreadsheet for Seamless Chart Creation
If your workplace prohibits macros or you find VBA workarounds inconvenient, consider switching to WPS Spreadsheet. It offers a robust, user-friendly charting environment without the heavy reliance on scripts to make basic visual adjustments.
- 1. Download and Install: Get WPS Office for free from the official website and install it on your device.
- 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing Excel workbook containing the PivotData.
- 3. Customize Your Chart: Use the intuitive chart formatting pane in WPS to effortlessly tweak your timeline axes and gridlines.

Frequently Asked Questions
Why don't my PivotChart gridlines align correctly by default?
Excel lacks native layout options for properly spacing gridlines on a multilevel category axis, such as when data is grouped by both month and year. This limitation causes standard intervals to shift inaccurately.
Do I have to run the VBA macro every time I update the chart?
Yes. The VBA macro draws physical line shapes over your chart based on its current dimensions and data. If you resize the chart window or refresh the PivotTable data, you must run the macro again to realign the lines.
Can I achieve this without VBA if my workplace restricts macros?
Unfortunately, standard Excel interval settings cannot perfectly align these gridlines for multilevel groupings. If macros are banned, you may need to simplify your grouping to single-level dates or use alternative software.
How do I save my workbook after adding the VBA macro?
To preserve the VBA macro for future use, you must save your file as an Excel Macro-Enabled Workbook. Go to 'File' > 'Save As' and select '.xlsm' from the format dropdown menu.




