adding a graph without incrementing the sheet codename

ks79

New Member
Joined
Jul 26, 2010
Messages
2
Hi,
I'm using the following macro to add multiple charts to a sheet named "charts". The macro is running fine and does what it is intended to do. Now the problem is that my workbook has a total of 3 worksheets but whenever I run the macro it seems to add a new sheet for each graph and then removes it. So this increments the sheet codename number and currently its at 183 (like sheet183). And if I'm not wrong then possibly has maximum limit on the number of sheets it can add and I would run out of it on a certain date. Is there a way to fix the issue. I'm quite new to vba, so any help is appreciated.

Thanks

Sub chartadd()

Dim lastrow As Long
Dim i As Integer

lastrow = Sheet1.Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:BN1,A" & i & ":BN" & i), PlotBy _
:=xlRows
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = "=Sheet1!R1C5:R1C35"
ActiveChart.SeriesCollection(1).Values = "=Sheet1!R" & i & "C5:R" & i & "C35"
ActiveChart.SeriesCollection(2).Values = "=Sheet1!R" & i & "C36:R" & i & "C66"
ActiveChart.SeriesCollection(2).Name = "=Sheet1!R" & i & "C1:R" & i & "C3"
ActiveChart.Location Where:=xlLocationAsObject, Name:="Charts"
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlTop
ActiveChart.HasDataTable = False
ActiveChart.Axes(xlCategory).Select
With Selection.TickLabels
.Alignment = xlCenter
.Offset = 100
.ReadingOrder = xlContext
.Orientation = xlUpward
End With
ActiveChart.PlotArea.Select
Selection.ClearFormats
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).AxisGroup = 2
ActiveChart.SeriesCollection(2).ChartType = xlArea
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).ChartType = xlColumnClustered
ActiveChart.SeriesCollection(1).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
Selection.InvertIfNegative = False
Selection.Fill.Patterned Pattern:=msoPattern90Percent
With Selection
.Fill.Visible = True
.Fill.ForeColor.SchemeColor = 7
.Fill.BackColor.SchemeColor = 2
End With
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).Select
With Selection.Border
.Weight = xlThin
.LineStyle = xlAutomatic
End With
Selection.Shadow = False
With Selection.Interior
.ColorIndex = 16
.Pattern = xlSolid
End With
ActiveChart.ChartArea.Select
ActiveWindow.Visible = False
'Windows("Sheet1").Activate
Range("A20").Select

Next i
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Is that code actually adding charts to a worksheet?

As far as I can see it's creating new chart sheets.

Do you actually use the codename anywhere?

If you don't then I wouldn't worry - there maybe be a limit but I've definitely never heard of one.

In fact I just created 1,000 charts with no problems, mind you they were blank.:)
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top