Scott
Scott

Reputation: 1

chart series data in first iteration of for loop vba

I am having an additional data series included in a chart that I don't intend to include. It is only showing up on the first iteration and no others. The value of the series is "={1}". How did this data series come to be included in the chart and how can it be removed?

 Set sh = Worksheets("LowDistCharts")
Set chrt = sh.ChartObjects.Add(0, 0, 300, 300)
Set ch = chrt.Chart

    With chrt
        .Height = 300
        .Width = 300
        .Top = 1 + ((aa - 4) * 300)
        .Left = 1
    End With

    With ch
        .HasTitle = True
        .ChartTitle.Text = aa & " " & StartDate & " to " & EndDate
        .ChartTitle.Font.Size = 8
        .ChartType = xlLine
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Values = ActiveWorkbook.Worksheets("ActiveSheet").Range(RngXR, RngXR2)
        '.SeriesCollection(1).AxisGroup = 1
        .SeriesCollection.NewSeries
        .SeriesCollection(2).Values = ActiveWorkbook.Worksheets("ActiveSheet").Range(RngStartR, RngEndR)
        .SeriesCollection(2).AxisGroup = 2
        .SeriesCollection(3).Delete
        .HasLegend = False
    End With

Upvotes: 0

Views: 40

Answers (1)

Tim Williams
Tim Williams

Reputation: 166306

When adding a chart you may find one or more series "auto-added" if there are cells with data selected when the chart is created.

You can make sure these are removed before adding the data you want:

Set chrt = sh.ChartObjects.Add(0, 0, 300, 300)
Set ch = chrt.Chart

Do While ch.SeriesCollection.Count > 1
    ch.SeriesCollection(1).Delete
Loop

Upvotes: 1

Related Questions