我目前正在尝试使用我预先构建的数组在 Excel 中创建堆积柱形图。数组不引用工作表上的任何范围,而是表示该数据的计算。

我遇到的问题是在创建堆积柱形图时,数据不是垂直堆积在同一列上,而是第二个数据集垂直堆积在第一个数据集附近。我将在下面附上一张图片,但现在让我向您展示我的代码,请注意,在这个子例程中我实际上创建了 4 个不同的图表,但其中只有一个需要是堆积柱,所以我将引用堆积柱代码如下:

Sub buildCharts()

    Dim myChart as Shape

    Set myChart = wsRawData.Shapes.AddChart2(Left:=792, Top:=0, Width:=264, Height:=192)

    With myChart.Chart

        'load the data
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Values = myArray1
        .SeriesCollection.NewSeries
        .SeriesCollection(2).Values = myArray2
        .SeriesCollection.NewSeries
        .SeriesCollection(3).Values = myArray3

        'x-axis
        .SeriesCollection(1).XValues = Array("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D")

        'set the chart type
        .FullSeriesCollection(1).ChartType = xlColumnStacked
        .FullSeriesCollection(1).AxisGroup = 1
        .FullSeriesCollection(2).ChartType = xlColumnStacked
        .FullSeriesCollection(2).AxisGroup = 1
        .FullSeriesCollection(3).ChartType = xlLine
        .FullSeriesCollection(3).AxisGroup = 1
        .FullSeriesCollection(3).Format.Line.Weight = 1.25

        'edit
        .ChartStyle = 209
        .HasTitle = True
        .chartTitle.Text = "My Chart"
        .ChartArea.Font.Color = vbWhite
        .ChartArea.Interior.ColorIndex = 1
        .HasLegend = True
        .Legend.Position = xlLegendPositionBottom
        .Axes(xlCategory).MajorGridlines.Delete
    End With

End Sub

这是上述代码的输出图像:

arrays - 在 VBA 中使用数组创建堆积柱形图-LMLPHP

如您所见,列是如何错误堆叠的。

现在,当我使用开发人员选项卡下的“记录宏”功能时,我得到以下代码:
ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
ActiveChart.FullSeriesCollection(1).AxisGroup = 1
ActiveChart.FullSeriesCollection(2).ChartType = xlColumnClustered
ActiveChart.FullSeriesCollection(2).AxisGroup = 1
ActiveChart.FullSeriesCollection(3).ChartType = xlLine
ActiveChart.FullSeriesCollection(3).AxisGroup = 1
ActiveChart.FullSeriesCollection(1).ChartType = xlColumnStacked

当使用数据手动创建图表时,它会创建完美堆叠的图表。

所以我不确定我错过了什么。我在网上做了一些挖掘,但一直找不到任何东西,希望这里有人能给我一个更好的理解!

提前致谢。

最佳答案

我稍微改变了你的代码(我在 Excel 2007 中工作)

   Dim myChart As Shape
    'changed addchart2 to addchart and sheet name, left etc
    Set myChart = Sheet1.Shapes.AddChart(Left:=92, Top:=0, Width:=264, Height:=192)
    With myChart.Chart

        'load the data
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Values = Array(100, 70, 120, 80, 40, 150, 200, 140, 150, 90, 110, 50)
        .SeriesCollection.NewSeries
        .SeriesCollection(2).Values = Array(100, 70, 120, 80, 40, 150, 200, 140, 150, 90, 110, 50)
        .SeriesCollection.NewSeries
        .SeriesCollection(3).Values = Array(150, 120, 150, 120, 80, 180, 280, 180, 195, 130, 160, 150)

        'x-axis
        .SeriesCollection(1).XValues = Array("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D")



        'set the chart type
        ' used .SeriesCollection instead of .FullSeriesCollection
        .SeriesCollection(1).ChartType = xlColumnStacked
        .SeriesCollection(1).AxisGroup = 1
        .SeriesCollection(2).ChartType = xlColumnStacked
        .SeriesCollection(2).AxisGroup = 1
        .SeriesCollection(3).ChartType = xlLine
        .SeriesCollection(3).AxisGroup = 1
        .SeriesCollection(3).Format.Line.Weight = 2.25

        'edit
        '.ChartStyle =209  ' commented out chart style
        .HasTitle = True
        .ChartTitle.Text = "My Chart"
        .ChartArea.Font.Color = vbWhite
        .ChartArea.Interior.ColorIndex = 1
        .HasLegend = True
        .Legend.Position = xlLegendPositionBottom
        .Axes(xlCategory).MajorGridlines.Delete
    End With

End Sub

它正在生成这个图表
arrays - 在 VBA 中使用数组创建堆积柱形图-LMLPHP

如果对您有帮助,我们会很高兴

关于arrays - 在 VBA 中使用数组创建堆积柱形图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52987776/

10-10 18:56