I need to create one chart per sheet in Excel. Here is my code:
Sheets(i).Activate
For Each cht In ActiveSheet.ChartObjects
cht.Delete
Next
'create chart
Set chtChart = ActiveSheet.ChartObjects.Add(Left:=75, Width:=300, Top:=75, Height:=300).Chart
With chtChart
.ChartType = xlXYScatterSmooth
Do While .SeriesCollection.Count <> 0
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
Loop
Set srsNew = .SeriesCollection.NewSeries
With srsNew
.XValues = "='" & Sheets(i).Name & "'!" & _
Sheets(i).Range(Range("K2"), Range("k2").End(xlDown)).Address
.Values = "='" & Sheets(i).Name & "'!" & _
Sheets(i).Range(Range("l2"), Range("l2").End(xlDown)).Address
End With
End With
For the first sheet this works but for the second, third … it does not. It issues the error 1004 “application defined or object defined error” in XValues or Values. I also noticed that if I introduce
range("K2")
outside the with block I get an error in the second, third …. sheets but not in the first one.
Any advise is most welcome
Regards
João
Ranges with sheets they belong to.SelectorActiveSheet.