I am trying to create a stacked column chart using excel vba.input data
Following mentioned below is my excel vba code for generating the stacked column chart for the corresponding input data.
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
But when i run this macro, the output i am getting is different. the problem is in the x axis. my x axis should be D0, D1,D2(as you can see in image "Output Expected". But it is different. i have also attached the output i am getting when i run the vba code (Second image).
I am not understanding why my x axis is getting changed which is indeed affecting the code and the output graph.
Manually when i do without using code then i am getting the correct output.
where am i going wrong?
Chartobject) - Shai Rado