(Solved) – VBA – Adding extra series to scattergraph

(solved)-–-vba-–-adding-extra-series-to-scattergraph

I am trying to create a code which adds series to a scatter-graph. The code runs but there is about 100 extra series of data added which were not specified. My vba skills are basic.

Dim DownSweep As Chart
Dim xrng As Range
Dim yrng As Range
Dim title As Range
Dim dsvt As Worksheet
Dim dst As Worksheet
Dim i As Integer

t = 1

CLEAN:
    If t < ActiveWorkbook.Charts.Count   1 Then
    If ActiveWorkbook.Charts(t).Name = "DownSweep Graph" Then
    Application.DisplayAlerts = False
    ActiveWorkbook.Charts("DownSweep Graph").Delete
    Application.DisplayAlerts = True
    t = t   1
    GoTo CLEAN
    End If
    End If

    Set dst = Worksheets("Template 2 - Down Sweep")
    Set dsvt = Worksheets("DownSweep ViscosityTemperature")

Set xrng = dsvt.Range(dsvt.Range("C2"), dsvt.Range("C2").End(xlDown))
Set yrng = dsvt.Range(dsvt.Range("F2"), dsvt.Range("F2").End(xlDown))
Set title = dsvt.Range("F1")

dsvt.Range("E1").Select

    Set DownSweep = Charts.Add
    DownSweep.Name = "DownSweep Graph"

    With DownSweep
            .ChartType = xlXYScatter
            .SeriesCollection.NewSeries
            .SeriesCollection(1).XValues = xrng
            .SeriesCollection(1).Values = yrng
            .SeriesCollection(1).Name = title
    End With

title = title.Offset(0, 1)

For i = 2 To 99
        With DownSweep.SeriesCollection.NewSeries()
            .XValues = xrng.Offset(0, i - 1).Value
            .Values = yrng.Value
            .Name = title
        End With
        title = title.Offset(0, i)
    Next i

End Sub

How do I prevent this from happening?

Any help would be appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *