Search code examples
excelexcel-chartsvba

how to add series to chart


I am trying add a new chart1 sheet and adding series from sheet1 to my chart1 sheet.

Here is my code...

Charts.Add
With ActiveChart
    .ChartType = xlLine
    .HasTitle = True
    .ChartTitle.Text = "Closing Price"
    .SeriesCollection.NewSeries
    .FullSeriesCollection.XValues = "=Sheet1!$A$2:$A$741" '<<<<This is the xvalue
    .FullSeriesCollection.Values = "=Sheet1!$B$2:$B$741"  '<<<<This are the value 
End With

Please tell me how to do this.. Thanks in Advance


Solution

  • You have to specify the number of the series collection.

    Charts.Add
    With ActiveChart
        .ChartType = xlLine
        .HasTitle = True
        .ChartTitle.Text = "Closing Price"
        .SeriesCollection.NewSeries
        .FullSeriesCollection(1).XValues = "=Sheet1!$A$2:$A$741" '<<<<This is the xvalue
        .FullSeriesCollection(1).Values = "=Sheet1!$B$2:$B$741"  '<<<<This are the value
    End With
    

    By the way, if you took the trouble to fire up the macro recorder, insert a new chart sheet, define new series, then stop the macro recorder, you would see all the commands laid out in full.