Create the Correct Stacked Bar Chart with Excel VBA
Question details
The user wants to create a stacked bar chart in Excel VBA with the source data plotted in the intended orientation.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A VBA macro creates a stacked bar chart, but the desired appearance depends on whether Excel plots the source data by rows or columns.
- Observed behavior
- Two charts can use the same stacked bar chart type while displaying different results because their rows and columns are plotted differently.
Arrange the chart data in a worksheet range and identify whether each row or each column should represent a series. Enable the Developer tab in Excel if you need access to the Visual Basic editor.
Set the chart type and PlotBy orientation in VBA
Use this method to create a stacked bar chart and explicitly control how Excel interprets the source data.
The chart type controls the stacked bar style; PlotBy controls whether rows or columns become data series. Adjust the source range to include your data and any labels.
In Excel, open the Developer tab, select Visual Basic, and choose Insert > Module.
Paste this code into the module: Sub CreateStackedBarChart() Dim wsh As Worksheet Dim cht As Chart Set wsh = ActiveSheet Set cht = wsh.ChartObjects.Add(10, 10, 400, 300).Chart With cht .ChartType = xlBarStacked .SetSourceData Source:=wsh.Range("A1:C4"), PlotBy:=xlRows End With End Sub
Replace A1:C4 with your source range. Keep PlotBy:=xlRows to use rows as series, or change it to PlotBy:=xlColumns to use columns as series.
Activate the worksheet containing the source data, then run CreateStackedBarChart from Developer > Macros. Check the chart's series and category labels; change PlotBy if they are reversed.

Create stacked bar charts in WPS Spreadsheets
WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Open compatible Excel files and create stacked bar charts without needing to migrate your data manually. WPS does not fix Excel's VBA settings, and macro compatibility can vary.

Frequently Asked Questions
Why do two stacked bar charts look different even with the same chart type?
Their source data may be plotted in different orientations. In SetSourceData, use PlotBy:=xlRows or PlotBy:=xlColumns to choose which direction supplies the series.
What is the VBA constant for a stacked bar chart?
Set the chart's ChartType property to xlBarStacked, which has the value 58. For a 100% stacked bar chart, use xlBarStacked100, value 59.
How do I switch rows and columns in an existing chart?
In VBA, set the chart's PlotBy property to xlRows or xlColumns. You can also select the chart in Excel and use Chart Design > Switch Row/Column.
Why does a recorded macro produce a chart with the wrong orientation?
The recorded chart type may be correct while Excel interprets the source range in the other orientation. Add PlotBy:=xlRows or PlotBy:=xlColumns to the SetSourceData call and check the resulting series.




