Changing Series Origin with VBA for excel

Franky Leonardo Prieto 1 Reputation point
2021-02-03T04:34:32.707+00:00

I need to create several charts with different data but the same format, for a report.
I duplicate the first chart and change the location and the series origin but there are different behavior. Some times I can change the series origin with

...
ChartObject(n).Chart.SeriesCollection(1).Values = Range(range 1st serie).Address(, , , True)
ChartObject(n).Chart.SeriesCollection(2).Values = Range(range 2nd sere).Address(, , , True)
...

But sometimes when it changes the first one, it deletes the second one and it generates an error when it tryes to changes origin from a non-existing serie.

How I can know when change the origin from the both series, and when delete the second?

If this always happen, I could write:

ChartObjects(n).Chart.SeriesCollection(1).Values = Range(range 1st serie).Address(, , , True)
ChartObjects(n).Chart.SeriesCollection.Add
ChartObjects(n).Chart.SeriesCollection(2).Values = Range(range 2nd sere).Address(, , , True)

But sometimes it deletes, sometimes it doesn´t

What would be the best way to do this?

0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.