+ Reply to Thread
Results 1 to 4 of 4

Thread: Creating a Chart

  1. #1
    Registered User
    Join Date
    10-28-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    2

    Creating a Chart

    I am designing a macro in order to load a range (Spanning columns a, b, and c, with a variable amount of rows), and came up with the following code. It seems to work fine using Excel 2003, but when running compatibility tests in Excel 2007, I received an error '400'. Thinking it was perhaps an incompatibility with the ChartObjects.Add command, I replaced it with the newer Shapes.AddChart command, but no luck.

    Any ideas? The chart code is below.

    Sub CreateChart()
    Dim rowMax As Integer
    
    rowMax = Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row 'UsedRange.Rows.Count
    
    With Sheets(1).ChartObjects.Add(Left:=5, Width:=614, Top:=477, Height:=462).Chart
        .SetSourceData Source:=Sheets(2).Range("A1", "C" & rowMax), PlotBy:=xlColums    
        .ChartType = xlLine
        .Parent.Name = "DataChart"
        .HasTitle = True
        .ChartTitle.Characters.Text = "Title"
        
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X Axis"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y Axis"
        
        .HasDataTable = False
        
        .Axes(xlCategory).HasMajorGridlines = False
        .Axes(xlCategory).HasMinorGridlines = False
        .Axes(xlCategory).TickLabelSpacing = Int(rowMax / 3)
        
        .Axes(xlValue).HasMajorGridlines = True
        .Axes(xlValue).HasMinorGridlines = False
    End With
       
    End Sub
    Last edited by dwaldon; 10-28-2010 at 07:40 PM.

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Creating a Chart

    Hi,

    Try the following slight modification. Incidentally, and I'm sure it was a typo but your original said 'PlotBy = xlColums' and not xlColumns and would have failed because of this.

    Sub CreateChart()
        Dim rowMax As Integer
    
        rowMax = Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row    'UsedRange.Rows.Count
    
        With Sheets(1).Shapes.AddChart.Select
            With ActiveChart
                .Parent.Left = 5
                .Parent.Width = 614
                .Parent.Top = 477
                .Parent.Height = 462
                .SetSourceData Source:=Range("A1:C" & rowMax)
                .ChartType = xlLine
                .Parent.Name = "DataChart"
                .HasTitle = True
                .ChartTitle.Characters.Text = "Title"
                .Axes(xlCategory, xlPrimary).HasTitle = True
                .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "X Axis"
                .Axes(xlValue, xlPrimary).HasTitle = True
                .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y Axis"
                .HasDataTable = False
                .Axes(xlCategory).HasMajorGridlines = False
                .Axes(xlCategory).HasMinorGridlines = False
                .Axes(xlCategory).TickLabelSpacing = Int(rowMax / 3)
                .Axes(xlValue).HasMajorGridlines = True
                .Axes(xlValue).HasMinorGridlines = False
    
            End With
        End With
    
    End Sub
    Last edited by Richard Buttrey; 10-28-2010 at 08:03 PM.
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Registered User
    Join Date
    10-28-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    2

    Re: Creating a Chart

    Thanks for your response .

    The error 400 is gone now... however, it's been replaced with a System Error &H80070057 (-2147024809). If it helps any, the error is occuring on line 3 (Sheets(1).Shapes.AddChart.Select).

    EDIT: That seems to be the result of some protections on the sheet, but even without the protections I'm getting an error at '.SetSourceData' (Object variable or With block variable not set)

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Creating a Chart

    Hi,

    Are you running the macro when the most left hand sheet is the active sheet? The macro refers to Sheets(1) which I always feel is somewhat dangerous in case a user drags the sheet to another position. Far better to explicitly refer to the sheet's name or code name.

    HTH
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

+ 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.2.0