How to Create an Excel Chart with Two Rows of Labels Using VBA
Question details
The user needs to create an Excel chart using VBA where two rows act as multi-level category labels, preventing the second row from being incorrectly plotted as data.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating chart creation with VBA after a recent Excel update broke the previous behavior of interpreting two rows as headers.
- Observed behavior
- The VBA macro incorrectly interprets the second row of header labels as a data series instead of chart category labels, even though manual adjustment works correctly.
Before modifying your VBA code, ensure your data is arranged contiguously with the two rows of headers directly above your data columns, and save a backup of your macro-enabled workbook.
Explicitly Set XValues and PlotBy in VBA
Use explicit VBA properties to define the category labels and plot orientation, preventing the charting engine from guessing the layout of your data range.
When creating charts via VBA, recent updates to the chart engine might cause it to misinterpret multi-row headers. The macro recorder often fails to capture the exact syntax needed for this scenario.
By explicitly defining the XValues property and setting PlotBy to xlColumns, you force the application to recognize both rows as category labels rather than plotting the second row as data.
Press Alt + F11 in Excel to open the Visual Basic for Applications editor. Navigate to Insert > Module to create a new script area.
Write the code to create the chart object and assign your primary data range (excluding the labels if needed). For example: Set cht = ActiveSheet.Shapes.AddChart2.Chart.
Add the line cht.PlotBy = xlColumns to your code to ensure the chart plots your data series vertically down the columns.
Explicitly define the multi-level category labels by assigning your two-row header range to the series. Use: cht.FullSeriesCollection(1).XValues = ActiveSheet.Range("A1:B2") (adjust the range to match your actual header cells).

Try WPS Office for Stable Charting and Data Visualization
If unexpected Microsoft Excel updates are breaking your VBA macros and chart labels, consider switching to WPS Office. It offers a free, lightweight, and highly compatible spreadsheet environment with stable charting features, a familiar UI, and seamless migration.
- 1. Download WPS Office: Visit the official WPS website and download the free, lightweight office suite.
- 2. Install and Open: Install the software and open WPS Spreadsheet, which features a familiar and intuitive user interface.
- 3. Load your Workbook: Open your existing .xlsx or .xlsm file containing your data and charts.
- 4. Create your Chart: Highlight your data and insert the chart directly; WPS handles standard Excel file structures seamlessly.

Frequently Asked Questions
Why does Excel plot my second row of labels as a data series?
This usually happens when the automatic chart wizard misinterprets the dimensions of your data range during VBA execution. Explicitly defining the XValues (category labels) range in your code prevents the engine from guessing the layout.
Can I fix the multi-level category labels without using VBA?
Yes. You can manually fix this by right-clicking the chart, selecting 'Select Data', clicking 'Edit' under the Horizontal (Category) Axis Labels, and highlighting both rows of your headers.
Does the VBA macro recorder capture the XValues setting for multiple rows?
Often, the macro recorder only captures the generic SetSourceData method but fails to record the specific assignment of multi-row labels to the XValues property, which requires you to manually add the code.
What is the purpose of the PlotBy = xlColumns property in this VBA code?
The PlotBy property tells the charting engine that your data series are organized in columns rather than rows. This setting is crucial for correctly aligning the data series with multi-row column headers.




