我试图在vba中获得excel堆积条形图,但是当我尝试我的结果变得多变?
我做错了什么?
我得到的预期结果和结果: Chart
VBA代码供您参考:
Sub createchart()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("High")
Dim chrt As Object
Set chrt = ws.Shapes.AddChart2(297, xlBarStacked)
With chrt.Chart
.SetSourceData Source:=ws.Range("E4:E7")
.HasTitle = True
.ChartTitle.Text = ws.Range("E3").Value
End With
End Sub
答案 0 :(得分:2)
您需要按行绘制以获得所需的输出。您可以尝试以下修改。
Sub createchart()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("High")
Dim chrt As Object
Set chrt = ws.Shapes.AddChart
With chrt.Chart
.SetSourceData Source:=ws.Range("E4:E7"), PlotBy:=xlRows
.HasTitle = True
.ChartTitle.Text = ws.Range("E3").Value
.ChartType = xlBarStacked
End With
End Sub