excel vba中{堆积条形图的第二种选择

时间:2018-05-11 12:27:27

标签: excel-vba vba excel

我试图在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

1 个答案:

答案 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