Consulting

Results 1 to 2 of 2

Thread: Add multiple series to graph using VBA

  1. #1
    VBAX Regular
    Joined
    Oct 2007
    Posts
    13
    Location

    Add multiple series to graph using VBA

    OK, I am trying to automatically add some data series to a chart using VBA. The data is found in range W148:AC158. Here is the code I have so far, but for some reason I am getting an Application-defined or object defined error on the line that has

    .SeriesCollection(i).Name = Worksheets("Inputs").Cells(n, 23).Text


    Here is my complete code:

    Dim n As IntegerDim i As Integer
    
    
    n = 148
        With ActiveChart
           
            For i = .SeriesCollection.Count To 11 Step 1
                If Sheets("Inputs").Cells(n, 25) = 0 Then
                Else
                .SeriesCollection.NewSeries
                .SeriesCollection(i).Name = Worksheets("Inputs").Range(Cells(n, 23)).Text
                .SeriesCollection(i).XValues = Worksheets("Inputs").Range(Cells(n, 28), Cells(n, 29)).Value
                .SeriesCollection(i).Values = Worksheets("Inputs").Range(Cells(n, 26), Cells(n, 27)).Value
                End If
            n = n + 1
            Next i
            
        End With
    Any thoughts?

  2. #2
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Could you remove range()? as cells() already gives the cell reference.

    I'm not convinced you need .text but I have left it in.


    Dim n As IntegerDim i As Integer


    n = 148
    With ActiveChart

    For i = .SeriesCollection.Count To 11 Step 1
    If Sheets("Inputs").Cells(n, 25) = 0 Then
    Else
    .SeriesCollection.NewSeries
    .SeriesCollection(i).Name = Worksheets("Inputs").Cells(n, 23).Text
    .SeriesCollection(i).XValues = Worksheets("Inputs").Range(Cells(n, 28), Cells(n, 29)).Value
    .SeriesCollection(i).Values = Worksheets("Inputs").Range(Cells(n, 26), Cells(n, 27)).Value
    End If
    n = n + 1
    Next i

    End With


    Not a quote.. wasn't displaying the code correctly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •