创建堆积柱形图的问题

时间:2016-08-02 08:16:44

标签: excel vba excel-vba

我正在尝试使用excel vba创建堆积柱形图。input data

下面提到的是我的excel vba代码,用于为相应的输入数据生成堆积柱形图。

      Sub to_draw_chart()
         ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
         ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$P$4")
         ActiveChart.Axes(xlValue).Select
         ActiveChart.Axes(xlValue).MaximumScale = 1000
         ActiveChart.Axes(xlValue).MajorUnit = 250

         ActiveChart.ChartArea.Select
         ActiveChart.Axes(xlCategory).Select

         ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
         ActiveChart.Axes(xlCategory).CategoryType = xlAutomatic
         Application.CommandBars("Format Object").Visible = False
         ActiveChart.FullSeriesCollection(2).Select
         ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
         Selection.Format.Fill.Visible = msoFalse
         ActiveChart.FullSeriesCollection(5).Select
         Selection.Format.Fill.Visible = msoFalse
         ActiveChart.FullSeriesCollection(12).Select
         Selection.Format.Fill.Visible = msoFalse
         ActiveChart.FullSeriesCollection(15).Select
         Selection.Format.Fill.Visible = msoFalse

         ActiveChart.ChartArea.Select
         ActiveChart.ChartTitle.Select
         ActiveChart.ChartTitle.Text = "Chart "
         Selection.Format.TextFrame2.TextRange.Characters.Text = "Chart "
      End Sub

outputExpected_VS_output_getting

但是当我运行这个宏时,我得到的输出是不同的。问题出在x轴上。我的x轴应该是D0,D1,D2(你可以在图像“输出预期”中看到。但它是不同的。我还附加了我在运行vba代码时获得的输出(第二张图像)。

我不明白为什么我的x轴会发生变化,这确实会影响代码和输出图。

当我不使用代码时手动,然后我得到正确的输出。

我哪里错了?

2 个答案:

答案 0 :(得分:1)

由于我没有使用您的某些代码行,因此不确定所提供代码的最终图表是什么。

还有另一种选择,一种“更清洁”,更有效的方式来处理图表(不需要一直选择它们,我想你是用MACRO记录器做的。)

无论如何,请参阅下面的代码,它给出了与手动步骤相同的确切结果(就像在附加的图像中一样)。

Option Explicit

Sub to_draw_chart()

Dim Sht1                   As Worksheet

' modify to your sheet name
Set Sht1 = ThisWorkbook.Sheets("Sheet1")

' change Left, Top, Width , Height according to your needs
Sht1.Shapes.AddChart(xlColumnStacked, 200, 200, 500, 500).Select

With ActiveChart
    .SetSourceData Source:=Range("Sheet2!$A$1:$P$4")
    .Axes(xlValue).MaximumScale = 1000
    .Axes(xlValue).MajorUnit = 250

    .HasTitle = True
    .ChartTitle.Text = "Chart "
    .ChartTitle.Format.TextFrame2.TextRange.Characters.Text = "Chart "

    .Axes(xlCategory).CategoryType = xlCategoryScale
    .Axes(xlCategory).CategoryType = xlAutomatic

' not sure what is the purpose with the lines below ?      
'         Application.CommandBars("Format Object").Visible = False
'         ActiveChart.FullSeriesCollection(2).Select
'         ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
'         Selection.Format.Fill.Visible = msoFalse
'         ActiveChart.FullSeriesCollection(5).Select
'         Selection.Format.Fill.Visible = msoFalse
'         ActiveChart.FullSeriesCollection(12).Select
'         Selection.Format.Fill.Visible = msoFalse
'         ActiveChart.FullSeriesCollection(15).Select
'         Selection.Format.Fill.Visible = msoFalse
End With   

End Sub

答案 1 :(得分:0)

您需要通过添加ActiveChart.PlotBy = xlColumns来切换行/列。以下内容应解决问题。

  Sub to_draw_chart()
     ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
     ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$P$4")
     ActiveChart.PlotBy = xlColumns
     ActiveChart.Axes(xlValue).Select
     ActiveChart.Axes(xlValue).MaximumScale = 1000
     ActiveChart.Axes(xlValue).MajorUnit = 250

     ActiveChart.ChartArea.Select
     ActiveChart.Axes(xlCategory).Select

     ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
     ActiveChart.Axes(xlCategory).CategoryType = xlAutomatic
     Application.CommandBars("Format Object").Visible = False
     ActiveChart.FullSeriesCollection(2).Select
     ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
     Selection.Format.Fill.Visible = msoFalse
     ActiveChart.FullSeriesCollection(5).Select
     Selection.Format.Fill.Visible = msoFalse
     ActiveChart.FullSeriesCollection(12).Select
     Selection.Format.Fill.Visible = msoFalse
     ActiveChart.FullSeriesCollection(15).Select
     Selection.Format.Fill.Visible = msoFalse

     ActiveChart.ChartArea.Select
     ActiveChart.ChartTitle.Select
     ActiveChart.ChartTitle.Text = "Chart "
     Selection.Format.TextFrame2.TextRange.Characters.Text = "Chart "
  End Sub