Modifying a VBA code

jondavis1987

Active Member
Joined
Dec 31, 2015
Messages
411
Office Version
  1. 2019
Platform
  1. Windows
So i recorded a macro for what I am trying to do. I have a worksheet called Superpave Template. I regularly have to create add a new sheet to the book and copy everything from the superpave template into the new sheet. The problem is the chart doesn't update automatically to the new sheet. It still pulls from the Superpave Template. That's what all of this is trying to do. keep getting an error on the line that i put asterisks on. The error code says object with specified name can't be found

VBA Code:
Option Explicit

Sub New_Superpave()
'
' New_Superpave Macro
'

'
    Sheets.Add After:=ActiveSheet
    Sheets("Superpave Template").Select
    Cells.Select
    Selection.Copy
    Sheets("Sheet3").Select
    ActiveSheet.Buttons.Add(891.75, 63, 91.5, 30).Select
    ActiveSheet.Paste
   * ActiveSheet.ChartObjects("Chart 1").Activate *
    ActiveChart.PlotArea.Select
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    ActiveChart.FullSeriesCollection(1).Values = "=Sheet3!$L$9:$L$19"
    ActiveChart.FullSeriesCollection(2).Values = "=Sheet3!$M$9:$M$19"
    ActiveChart.FullSeriesCollection(3).Values = "=Sheet3!$O$9:$O$19"
    ActiveChart.FullSeriesCollection(4).Values = "=Sheet3!$P$9:$P$19"
End Sub
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,844
Try replacing all that code with

VBA Code:
Sub New_Superpave2()
    Sheets("Superpave Template").Copy After:= ActiveSheet
End Sub
 

jondavis1987

Active Member
Joined
Dec 31, 2015
Messages
411
Office Version
  1. 2019
Platform
  1. Windows
Second time today I felt stupid but I am terrible with vba. Thanks!
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
909
Office Version
  1. 2010
hi, mickerckson nice code but if there are way if change data in activesheet and copy to after activesheet not every time update data add a new sheet it adds many sheets maybe 50 sheets if i have changes data so i supposing updating is the same sheet is ok
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,844
I'm not sure if I understand what you want.
But if you have a template sheet and you want to copy that sheet and then change some of data on the new sheet, you could then copy, not the original template sheet, but the new one 50 times.
Or are there 50 different variations you want to make all based on data from the ActiveSheet? If so something like

VBA Code:
Dim homeSheet as worksheet

Set homeSheet = ActiveSheet

For i = 1 to 50
    Sheets("Template").Copy after:=HomeSheet 
    ActiveSheet.Range("A1") = homeSheet.Cells(i,2)
Next i
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
909
Office Version
  1. 2010
sorry mickerckson if i don't make clear enough you misunderstood me i don't want create 50 sheets when create a new sheet after main sheet it doesn't suppose creating a new sheets every time change data in main sheet just updating data which is created new sheet
 

Watch MrExcel Video

Forum statistics

Threads
1,118,750
Messages
5,574,007
Members
412,562
Latest member
woodportaj
Top