Change data range in a chart using VBA - excel

It has been hours that I'm struggling with what I think to be a sible problem since Im not familiar at all with Chart object in VBA.
Here's my chart:
What I want to do: Change the data range of the two axis, the problem is that cant figure out a way to edit the series of the chart.
Thanks four your help !

You could use
With Workbooks(bookname).Charts(Chartname)
.SeriesCollection.NewSeries
.SeriesCollection(i).XValues = YourXs
.SeriesCollection(i).values = YourYs
end with
You can choose which series you want to edit by using the index i. This actually sets the (X,Y) pairings while what's below only changes the range shown by the graph.
To change the bounds of your Y axis you can use
.Axes(xlValue).MinimumScale =
.Axes(xlValue).MaximumScale =
To change the bounds of your x axis use
.Axes(xlCategory).MinimumScale =
.Axes(xlCategory).MaximumScale =

You said you wanted to change
=SERIES("SO2 U5",'CEMS_U6_YTD 2018'!$B$2165:$B$2303,'CEMS_U5_YTD 2018'!$D$2165:$D$2312,1)
to this
=SERIES("SO2 U5",'CEMS_U6_YTD 2018'!$C$2165:$C$2303,'CEMS_U5_YTD 2018'!$D$2165:$D$2312,1)
This may be a simple as
ActiveChart.SeriesCollection(1).Formula = _
"=SERIES(""SO2 U5"",'CEMS_U6_YTD 2018'!$C$2165:$C$2303,'CEMS_U5_YTD 2018'!$D$2165:$D$2312,1)"
note doubling of the double quotes around the series name.
However, since you are only changing the X values, you could use this:
ActiveChart.SeriesCollection(1).XValues = "='CEMS_U6_YTD 2018'!$C$2165:$C$2303"
or
ActiveChart.SeriesCollection(1).XValues = Worksheets("CEMS_U6_YTD 2018").Range("$C$2165:$C$2303")
I've written a tutorial about editing series formulas with VBA: Change Series Formula – Improved Routines.

Related

Excel VBA graph based on Series not working properly

I'm using the following VBA code in Excel in order to produce 2 graphs with a data picked up from the sheet and slightly elaborated. As I'm not using ranges directly for the graphs and need to use arrays for X and Y axes, I decided to go for Series. X has Date type and Y has Integer type. First it worked, but then the graphs started giving me wrong data, something random coming directly off the sheet. I suppose there could be something wrong in the way how the charts are declared. Here's the code, what could go wrong with it?
Dim crt1, crt2 As Chart
Dim sr1, sr2 As Series
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered, 60, 50).Select
Set crt1 = ActiveChart
Set s1 = crt1.SeriesCollection.NewSeries()
sr1.Values = dataArray1
sr1.XValues = datesArray1
ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
Set crt2 = ActiveChart
Set s2 = crt2.SeriesCollection.NewSeries()
sr2.Values = dataArray2
sr2.XValues = datesArray2
This problem also persists when I comment the code for the second graph.
P.S. Excel says "Runtime error 424. Object required" underlining "sr1.Values = dataArray1" and then "sr1.XValues = datesArray1"

How to change the subtype of Chart from VBA code

I create charts from some data:
ActiveSheet.Shapes.AddChart2(297, xlColumnStacked).Select
But on Excel there are different subtypes of the xlColumnStacked chart...
I need to show the second one, not the default one.
It has to be programmatically, not manually.
I tried to record the macro and see what does the change, but it doesn't record anything... I tried loading from Templates also but they don't work exactly like the one I need.
Thanks for your support.
I thing you have to change .ChartTyp
for example:
ActiveChart.ChartType = 52
Here is list of proper Value
https://bettersolutions.com/excel/charts/vba-chart-types.htm
If you want to switch x axis and y axis data try one of this:
ActiveChart.PlotBy = xlColumns
ActiveChart.PlotBy = xlRows
I hope I helped :)
They both looked like stacked columns. VBA doesn't have chart subtypes, and controls the chart type using this syntax:
ActiveChart.ChartType = xlColumnClustered
ActiveChart.ChartType = xlColumnStacked
ActiveChart.ChartType = xlColumnClusteredStacked100
VBA's Object Browser shows you all of the possibilities.

Set min/max value of the X and Y axes in an excel chart through vb.net

As the Title suggest, I need to find the way to set the maximum and minimum value for the X and Y axes of a chart created through a vb.net code. This is the part of the code it should be inserted in:
With ws
.Shapes.AddChart(Excel.XlChartType.xlXYScatterSmoothNoMarkers, 300, 20, 400, 300).Select()
With xlApp.ActiveChart
.SeriesCollection(1).Name = "Force vs Displacement"
.SeriesCollection(1).XValues = "='Sheet" & index & "'!$B$6:$B$14046"
.SeriesCollection(1).Values = "='Sheet" & index & "'!$C$6:$C$14046"
.HasLegend = False
[...]
Thanks for any suggestion made.
Sorry everybody. I finally found the correct page to look at, now almost everything is clear. The code for me is:
xlApp.ActiveChart.Axes(2, 1).maximumscale = 1.2
xlApp.ActiveChart.Axes(2, 1).minimumscale = -0.2
where in brackets are XlAxisType and XlAxisGroup. 2 is for xlValue, whereas the 1 is for xlPrimary. Here you can find all I needed to not post my question:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.axes?view=excel-pia
Sorry once more. See ya!

Excel VBA Chart Positions not constant

I have a worksheet where I create graphs in VBA and then place them according to named ranges in the worksheet.
graph3.Height = gRange.Height
graph3.Width = gRange.Width
graph3.Top = gRange.Cells(1, 1).Top
graph3.Left = gRange.Cells(1, 1).Left
It used to work fine but for some reason the graph no longer is positioned in the correct location. When I set a a stop point on any of the four lines and then continue to run the procedure, the graph returns to the correct position. Has anyone ever experienced this and how can I correct it?

In an Excel chart: how to "copy" chart line format from one series to another using VBA?

For some x and y such that SeriesCollection(x) exists and SeriesCollection(y) exists, I am trying to do the following:
ActiveChart.SeriesCollection(x).Format.Line = ActiveChart.SeriesCollection(y).Format.Line
But, that gives me an error: "object doesn't support this property or method". How can I copy the line format of one series from a chart, to another series in the chart?
You have to iterate through each property of .Format.Line
(It's confusing to use X and Y as variables indicating series, since X and Y conventionally refer to data of those series.)
Set srsA = ActiveChart.SeriesCollection(A)
Set srsB = ActiveChart.SeriesCollection(B)
srsA.Format.Line.ForeColor = srsB.Format.Line.ForeColor
srsA.Format.Line.Weight = srsB.Format.Line.Weight
etc.

Resources