Problem creatting charts with VBA
Well, through a lot of trial and error I figured a work around for the
SeriesCollection problem and the fatal Excel crash.
First regarding the crash, it turns out if the Excel tab is set to a chart
and you delete it through VBA, Excel crashes. In my subroutine that deletes
old charts I simply change the focus to a sheet first thats not being deleted.
Regarding the SeriesCollection I developed a subroutine that clears the
SeriesCollection and either returns the first series or creates a new one.
SeriesCollection of a new chart is unpredicable. Some times it starts with
one series, sometimes many and sometimes none.
Private Function GetNewSeries(cChart As Chart) As Series
Dim i As Integer
For i = cChart.SeriesCollection.Count To 2 Step -1
cChart.SeriesCollection(i).Delete
Next
If cChart.SeriesCollection.Count = 1 Then
Set GetNewSeries = cChart.SeriesCollection(1)
Else
Set GetNewSeries = cChart.SeriesCollection.NewSeries
End If
End Function
"TheWizEd" wrote:
In Excel 2007, when I create a new chart using Charts.Add I get a chart that
has all the series of a previously created chart. How can I prevent this? I
want a clean slate. I know that in the past I could not avoid having at
least one series when creating a chart. I simply redefine the attributes of
series one. And this seems to be true of 2007 as well.
|