+ Reply to Thread
Results 1 to 3 of 3

Setting a data range for a chart (error 1004).

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-14-2008
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    103

    Setting a data range for a chart (error 1004).

    Hi
    I'm trying to do a simple loop which creates charts based on an ID number. I recorded a macro and has tried to modify it but am having trouble defining the correct reange when settign the data source.

    Here is my code:

    Sub LapseGraphs()
    
    Dim i As Long, m As Integer, n As Integer
    
    m = 2 'row number start of current block
    For i = 2 To 60000
        If Cells(i, 1) <> Cells(i + 1, 1) Then
            n = i
        
        Range(Cells(m, 2), Cells(n, 3)).Select
        Charts.Add
        ActiveChart.ChartType = xlXYScatterLines
        ActiveChart.SetSourceData Source:=Sheets("Lapsed NSL").Range(Cells(m, 2), Cells(n, 3)), _ 'getting run time error 1004 at this line?
            PlotBy:=xlColumns
        ActiveChart.Location Where:=xlLocationAsObject, Name:="Lapsed NSL"
       
        m = n + 1
        End If
        
    Next i
    
    
    End Sub
    This results at a run time error 1004.
    Any help debugging this would be appreciated.
    Last edited by HammerTime; 02-02-2010 at 12:05 AM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Setting a data range for a chart (error 1004).

    Hi HammerTime,

    I don't think you can use the cells method when setting the source area for a chart. See how this goes:

    Sub LapseGraphs()
    
        Dim i As Long, m As Integer, n As Integer
        
        m = 2 'row number start of current block
        
        For i = 2 To 60000
            If Cells(i, 1) <> Cells(i + 1, 1) Then
                n = i
                    Charts.Add
                        With ActiveChart
                            .ChartType = xlXYScatterLines
                            .SetSourceData Source:=Sheets("Lapsed NSL").Range("B" & m & ":C" & n), _
                                PlotBy:=xlColumns
                            .Location Where:=xlLocationAsObject, _
                                Name:="Lapsed NSL"
                        End With
                m = n + 1
            End If
        Next i
    
    End Sub
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,441

    Re: Setting a data range for a chart (error 1004).

    You need to fully qualify the references.

        ActiveChart.SetSourceData Source:=Sheets("Lapsed NSL").Range(Sheets("Lapsed NSL").Cells(m, 2), Sheets("Lapsed NSL").Cells(n, 3)), PlotBy:=xlColumns
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1