+ Reply to Thread
Results 1 to 3 of 3

Thread: Creating Chart Programatically, error 1004

  1. #1
    Registered User
    Join Date
    10-20-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Creating Chart Programatically, error 1004

    I'm trying to create a chart programatically but I'm getting an error I'm stuck on:

    Run-time error '1004':
    The maximum number of data series per chart is 255.
    In the debugger, this error points to the line:

    Set myChart = Charts.Add
    Here's the code. I have verified with MsgBox() that the input variables to the subroutine are:

    rangeStr = "K231:N231"
    data1Str = "K232:N232"
    data2Str = "K233:N233"
    nameStr = "Historic Student Term Scores"
    yPosition = 50
    'Code by Mahipal Padigela
    'Open Microsoft Excel
    'Goto VBA Editor (Press Control+F11 or navigate Menu  Tools-->Macro-->Visual BAsic Editor)
    'Insert a Module (Right click your file name in the VB editor-->Insert-->Module)
    'Paste the following code into the Module1
    Sub addChart(rangeStr As String, data1Str As String, data2Str As String, nameStr As String, yPosition As Integer)
        
        Dim myChart As Excel.Chart
        Dim xlSheet As Excel.Worksheet
        
        Set xlSheet = ActiveWorkbook.Sheets("Student")
        'add chart
        Set myChart = Charts.Add
        'set chart type for e.g. xlPie,xlLine,xlArea,xl3DColumn etc
        myChart.ChartType = xlColumnClustered
        'set data range
        myChart.SetSourceData Source:=Sheets("Cohort").Range(rangeStr), PlotBy:=xlColumns
        'add the students data
        myChart.SeriesCollection(1).Values = Worksheets("Cohort").Range(data1Str)
        myChart.SeriesCollection(1).Name = Worksheets("Cohort").Range("A1")
        'add the cohort's data
        myChart.SeriesCollection.NewSeries
        myChart.SeriesCollection(2).Values = Worksheets("Cohort").Range(data2Str)
        myChart.SeriesCollection(2).Name = "Class"
        'Turn off the major gridlines for both axes
        myChart.Axes(xlCategory).HasMajorGridlines = False
        myChart.Axes(xlValue).HasMajorGridlines = False
        'Turn on the Legend and position it on top of the chart
        myChart.HasLegend = True
        myChart.Legend.Position = xlLegendPositionRight
        'add a title
        myChart.Name = nameStr
        myChart.HasTitle = True
        myChart.ChartTitle.Text = nameStr
    
        'Finally, which sheet you want the chart on
        myChart.Location xlLocationAsObject, "Student" 'This adds a standard sized chart to sheet2, but if you want to add a
        ' new sheet with just chart on it, replace the above line with next line...
        'myChart.Location xlLocationAsNewSheet, "Mychartsheet"
        'If you want specify the height and width of the chart object.......
        ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleWidth 1, msoFalse, msoScaleFromTopLeft
        ActiveSheet.Shapes(ActiveChart.Parent.Name).ScaleHeight 0.9, msoFalse, msoScaleFromTopLeft
        'move the chart into position
        With ActiveChart.Parent
            .Top = yPosition ' resize
            .Left = 10 ' resize
        End With
      
       Set myChart = Nothing
    End Sub
    Any ideas? I'm pretty new to macros and functions in excel and I'm totally stuck. Thank you very much for your help

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,351

    Re: Creating Chart Programatically, error 1004

    when you add the chart excel will use the data in the current region. Sounds like the active cell is in a data set that has more than 255 rows of data.

    Try setting the active cell to one without data around it.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    10-20-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Creating Chart Programatically, error 1004

    Thank you so much, Andy! You're a genius

    For anyone else reading this, I just added this line:

        ActiveSheet.Cells(1, 1).Select
    I put it right before the Charts.Add command, like so:

        Set xlSheet = ActiveWorkbook.Sheets("Student")
        'add chart
        ActiveSheet.Cells(1, 1).Select
        Set myChart = Charts.Add

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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