I'm trying to create a chart programatically but I'm getting an error I'm stuck on:
In the debugger, this error points to the line:Run-time error '1004': The maximum number of data series per chart is 255.
Here's the code. I have verified with MsgBox() that the input variables to the subroutine are:Set myChart = Charts.Add
rangeStr = "K231:N231" data1Str = "K232:N232" data2Str = "K233:N233" nameStr = "Historic Student Term Scores" yPosition = 50Any ideas? I'm pretty new to macros and functions in excel and I'm totally stuck. Thank you very much for your help'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
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.
Thank you so much, Andy! You're a genius
For anyone else reading this, I just added this line:
I put it right before the Charts.Add command, like so:ActiveSheet.Cells(1, 1).Select
Set xlSheet = ActiveWorkbook.Sheets("Student") 'add chart ActiveSheet.Cells(1, 1).Select Set myChart = Charts.Add
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks