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
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,767
Office Version
365
Platform
Windows
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.:)
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,896
Messages
5,514,029
Members
408,980
Latest member
Cooksey19

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top