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
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