Results 1 to 6 of 6

Thread: [RESOLVED] Setting Chart source to a Range fails.

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2015
    Location
    Cambridgeshire, UK
    Posts
    49

    Resolved [RESOLVED] Setting Chart source to a Range fails.

    I have this code:
    Code:
    With ActiveWorkbook.ActiveSheet
                    .Cells(1, 1) = "Time"
                    .Cells(1, 2) = "Temperature"
                    .Cells(1, 3) = "Target"
                    .Name = Format(Now, "dd-MMM-yyyy")
                    .ListObjects.Add(xlSrcRange, Range("A1:C2"), , xlYes).Name = "Table1"
                    Set cht = ActiveSheet.ChartObjects.Add(Left:=ActiveSheet.Cells(2, 5).Left, Width:=750, Top:=ActiveSheet.Cells(2, 5).Top, Height:=250)
                    With cht
                        .Chart.ChartType = xlXYScatterLines
                        .Chart.SetSourceData ActiveSheet.Range("Table1[#All]")
                        .Chart.SetSourceData ActiveSheet.Range("A1:C2")
                        .Chart.HasTitle = True
    The first line that tries .Chart.SetSourceData always fails, so I have to comment it out, and allow the second line instead.

    I see:
    Run-time error "1004"
    "Application-defined or object-defined error."

    I am trying to ensure the chart automatically updates when new data is added to the Table1 cells, but unsuccessfully!

    Any help would be much appreciated.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Setting Chart source to a Range fails.

    Have you tried the UsedRange-Property instead?
    Code:
    .Chart.SetSourceData ActiveSheet.UsedRange
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2015
    Location
    Cambridgeshire, UK
    Posts
    49

    Re: Setting Chart source to a Range fails.

    Found an answer!
    Code:
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Format(Now, "dd-MMM-yyyy")
                With ActiveSheet
                    .Cells(1, 1) = "Time"
                    .Cells(1, 2) = "Temperature"
                    .Cells(1, 3) = "Target"
                    .ListObjects.Add(xlSrcRange, Range("A1:C2"), , xlYes).Name = "Table"
                    Set cht = ActiveSheet.ChartObjects.Add(Left:=ActiveSheet.Cells(2, 5).Left, Width:=750, Top:=ActiveSheet.Cells(2, 5).Top, Height:=250)
                    With cht
                        .Chart.ChartType = xlXYScatterLines
                        .Chart.SetSourceData Source:=Range("Table[#All]")
                        .Chart.HasTitle = True
    This actually works!
    Thanks.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2015
    Location
    Cambridgeshire, UK
    Posts
    49

    Talking Re: Setting Chart source to a Range fails.

    Found an answer!
    Code:
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = Format(Now, "dd-MMM-yyyy")
                With ActiveSheet
                    .Cells(1, 1) = "Time"
                    .Cells(1, 2) = "Temperature"
                    .Cells(1, 3) = "Target"
                    .ListObjects.Add(xlSrcRange, Range("A1:C2"), , xlYes).Name = "Table"
                    Set cht = ActiveSheet.ChartObjects.Add(Left:=ActiveSheet.Cells(2, 5).Left, Width:=750, Top:=ActiveSheet.Cells(2, 5).Top, Height:=250)
                    With cht
                        .Chart.ChartType = xlXYScatterLines
                        .Chart.SetSourceData Source:=Range("Table[#All]")
                        .Chart.HasTitle = True
    This actually works!
    Thanks.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2015
    Location
    Cambridgeshire, UK
    Posts
    49

    Re: Setting Chart source to a Range fails.

    Now I have a new problem.
    I need to be able to name the new table, on each new sheet, uniquely, so the chart then references the correct data.
    Currently, the data named "Table" refers to the sheet on which it was created.
    I've tried naming the new data range according to the date, i.e. "28-01-2019", which works, and creates the new range OK, but then I cannot use the .Chart.SetSourceData command!
    It may just be my syntax:
    Code:
    .ListObjects.Add(xlSrcRange, Range("A1:C2"), , xlYes).Name = Format (Now, "dd-mm-yyyy")
    rangename$=Format (Now, "dd-mm-yyyy")
    
    
    .Chart .SetSourceData Source:=Range("" & rangename$ & "[#All]")
    This throws up the error:Runtime error "1004" Method 'Assistant' of object '_Global' failed.

    Can anyone throw some light on this, please?

    Alternatively, can the SetSourceData command be set to point to the active sheet?

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2015
    Location
    Cambridgeshire, UK
    Posts
    49

    Lightbulb Re: Setting Chart source to a Range fails.

    Well, after a fair bit of fiddling, I finally got it to work!

    Code:
                    .ListObjects.Add(xlSrcRange, Range("A1:C2"), , xlYes).Name = "Table" & Sheets.Count
                    Set cht = ActiveSheet.ChartObjects.Add(Left:=ActiveSheet.Cells(2, 5).Left, Width:=750, Top:=ActiveSheet.Cells(2, 5).Top, Height:=250)
                    With cht.Chart
                        .ChartType = xlXYScatterLines
                        .SetSourceData Source:=Range("Table" & Sheets.Count & "[#All]")
    It was just a matter of getting the syntax correct!

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width