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

标签 arrays excel vba charts

我目前正在尝试使用预先构建的数组在 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

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

enter image description here

正如您所看到的,列的堆叠方式不正确。

现在,当我使用开发人员选项卡下的“记录宏”功能时,我得到以下代码:

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

它正在生成这个图表
this chart

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

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

相关文章:

java - 如何将元素放入字符串数组中?

php - 如何使用逗号分隔值将数据库值存储在数组中

excel - VBA Excel 的比率条件要求

html - VBA 函数不返回 HTMLElementCollection 对象

api - 访问SysTreeView32的子节点

arrays - 使用具有命名范围的数组加速 VBA

arrays - Matlab:如何生成 0x0 结构数组?

php - 动态数组再次覆盖

excel - 如何在窗体窗口打开时保持 VBA 中的代码运行?

excel - Excel中=++C5是什么意思?