Vba 创建堆叠柱形图时出现问题
我正在尝试使用excel vba创建堆叠柱形图 下面提到的是我的excel vba代码,用于为相应的输入数据生成堆叠柱状图Vba 创建堆叠柱形图时出现问题,vba,excel,Vba,Excel,我正在尝试使用excel vba创建堆叠柱形图 下面提到的是我的excel vba代码,用于为相应的输入数据生成堆叠柱状图 Sub to_draw_chart() ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select ActiveChart.SetSourceData Source:=Range("Sheet2!$A$1:$P$4") ActiveChart.Axes(
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
但是当我运行这个宏时,我得到的输出是不同的。问题出在x轴上。我的x轴应该是D0、D1、D2(正如您在图像“预期输出”中看到的那样)。但它不同。我还附加了运行vba代码时获得的输出(第二幅图像)
我不明白为什么我的x轴被改变了,这确实影响了代码和输出图形
在不使用代码的情况下手动执行时,我将获得正确的输出
哪里出了问题?您需要通过添加
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
由于我没有使用你的一些代码行,所以我不确定你想要的最终图表是什么 还有另一种选择,一种“更干净”和更有效的方式来处理图表(不需要一直选择它们,我想你是用宏记录器完成的) 无论如何,请看下面我的代码,它给了我与手动步骤一样的精确结果(就像在您附加的图像中一样)
请参阅下面我的答案(另一个处理
图表
对象的选项)是的,它成功了。非常感谢。然后请向上投票并标记为答案
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