问题与创build堆积柱形图

我正在尝试使用excel vba创build一个堆积的柱状图。 input数据

下面提到的是我的excel vba代码,用于为相应的input数据生成堆栈柱状图。

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

但是,当我运行这个macros,我得到的输出是不同的。 问题在x轴上。 我的X轴应该是D0,D1,D2(正如你可以在图像“Output Expected”中看到的那样,但是不同的是,当我运行vba代码时(第二个图像),我还附加了输出。

我不明白为什么我的x轴正在变化,这确实影响到代码和输出graphics。

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

我哪里错了?

您需要通过添加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 

由于我没有使用一些代码行,所以不知道你所提供的代码是什么样的最终图表。

还有一个select,一个“更干净”,更有效的方式来处理图表(没有必要一直select它们,我想你是用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