[RESOLVED] excel VBA multiple chart problem
Currently I have a number of checkboxes, which the user clicks to graph certain data. Obviously in some instances, more than one checkbox is checked, so I would like to graph more than one graph, however with the following code I have, every graph after the first one (which works fine) fails with run-time error 1004 "Application - defined or Object- defined error". The line .setSourceData is highlighted with that error. Any help? Do I need to make a chartObjects?
Thanks much.
Code:
Sub AddChartSheet(chartName As String, setRange As String, yTitle As String, graphStyle As Integer)
Dim chtChart as Chart
Set chtChart = Charts.Add
With chtChart
.Name = chartName
If graphStyle = 2 Then
.ChartType = xlColumnClustered
ElseIf graphStyle = 1 Then
.ChartType = xlLine
End If
.SetSourceData Source:=Sheets("SOILSYM_MONTH").Range(setRange), _
PlotBy:=xlColumns
'Link to the source data range.
.HasTitle = True
.ChartTitle.Text = chartName
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Month"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = yTitle
.SeriesCollection(1).Delete
End With
End Sub
Re: excel VBA multiple chart problem
Hi
I could be wrong but
Whilst the code stops at ".SetSourceData " with this error it is probably NOT due to that. The other reason that I can think of is because of "chartName"
It is possible that while creating a chart you are not naming the chart correctly so the first time you create a chart using a name say "xyz", you cannot use the same name again. If you do then you will get the error which you are getting...
Try changing the name of all the charts that you are creating for example
vb Code:
If CheckBox1.Value = "aaa" Then 'just an example
.Name = "aaa"
ElseIf CheckBox1.Value = "bbb" Then 'just an example
.Name = "bbb"
End If
I think this should solve the problem... but like I said, I could be wrong...
Re: excel VBA multiple chart problem
Hrmm I don't think that is the problem because I pass "chartName" into the method and set .name = to that string, and I don't pass the same name twice into the method.
Re: excel VBA multiple chart problem
and also I think you get a slightly different error msg if you try to name a sheet the same name as another.
Re: excel VBA multiple chart problem
actually figured it out... had to do with not resetting my range variable. So on multiple graphs range kept getting appended new info, making it an illegal range. :sick:
thanks for the help though