After copying sheet and renaming them, removing formulas and keeping values for ONLY newly created sheets

leever

New Member
Joined
Jun 29, 2023
Messages
9
Hello, I have a workbook with two worksheets.

Worksheet 1 is [Template] – contains formulas
Worksheet 2 is [Cities] – contains list of cities in column A (Toronto, Vancouver, Montreal, Ottawa, Calgary)

With my code, I have successfully copied the template and renamed the sheets using the list from [Cities]. I am now trying to remove the formulas and keep the values for only the new sheets.

After I run the code, this is what occurs.
[Template] – formulas removed
[Cities] – N/A
[Toronto] – formulas stay
[Ottawa], [Vancouver], [Montreal], [Calgary] – formulas removed

What I want:
[Template] – formulas stay
[Cities] – N/A
[Toronto] – formulas removed
[Ottawa], [Vancouver], [Montreal], [Calgary] – formulas removed

This is my code.
---
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
Sheets("Template").Copy After:=Sheets(Sheets.Count)
Sheets("Template").UsedRange.Value = Sheets("Template").UsedRange.Value
ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i
End Sub
---

Any help would be much appreciated. Thank you.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try:
Rich (BB code):
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.UsedRange.Value = Sheets("Template").UsedRange.Value
    ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i
End Sub
 
Upvote 0
Try:
Rich (BB code):
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.UsedRange.Value = Sheets("Template").UsedRange.Value
    ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i
End Sub
This worked! Thank you so much!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
Hello. I spoke too soon. What ends up happening with the code above, the [Template] values get pasted into the new sheets - [Toronto], [Ottawa], [Vancouver], [Montreal], [Calgary].
The [Template] sheet has formulas that are tied to the sheet name. After the [Toronto], [Ottawa], [Vancouver], [Montreal], [Calgary] sheets are created. I want these formulas removed and sheet values pasted. Any further assistance would be much appreciated. Thank you!
 
Upvote 0
I want these formulas removed and sheet values pasted. Any further assistance would be much appreciated. Thank you!
Do you mean you want to remove formulas in sheet Template & keep the values?

Rich (BB code):
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.UsedRange.Value = Sheets("Template").UsedRange.Value
    ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i
Sheets("Template").UsedRange.Value = Sheets("Template").UsedRange.Value
End Sub
 
Upvote 0
Hi there, thank you again for your help with this.

For [Template], I want to keep the formulas and keep values. In this sheet, I have formulas that are calculated based on the sheet name.
For example, when [Toronto] sheet is created, it will make calculations for [Toronto] results only because sheet name is [Toronto].

With the code you provided this is what is happening:
[Template] – formulas removed and [Template] values pasted
[Cities] – N/A
[Toronto] – formulas removed and [Template] values pasted
[Ottawa] – formulas removed and [Template] values pasted
[Vancouver] – formulas removed and [Template] values pasted
[Montreal] – formulas removed and [Template] values pasted
[Calgary] – formulas removed and [Template] values pasted

This is what I am looking for:
[Template] – formulas stay and [Template] values kept
[Cities] – N/A
[Toronto] – formulas removed and [Toronto] values pasted
[Ottawa] – formulas removed and [Ottawa] values pasted
[Vancouver] – formulas removed and [Vancouver] values pasted
[Montreal] – formulas removed and [Montreal] values pasted
[Calgary] – formulas removed and [Calgary] values pasted

Thank you again.
 
Upvote 0
This is what I am looking for:
[Template] – formulas stay and [Template] values kept
Sorry for the late reply, try:
VBA Code:
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    [B]ActiveSheet[/B].UsedRange.Value = Sheets("Template").UsedRange.Value
    ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i

End Sub
 
Upvote 0
Sorry for the late reply, try:
VBA Code:
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    [B]ActiveSheet[/B].UsedRange.Value = Sheets("Template").UsedRange.Value
    ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i

End Sub
Hello, Thank you again for your help.

Using the code above, the " [B ] ActiveSheet [ / B].UsedRange.Value = Sheets("Template").UsedRange.Value "

shows as red and I get a "Compile error: Syntax error" message.

Any further help would be greatly appreciated. Thank you.


Erica
 
Last edited:
Upvote 0
Using the code above, the " [B ] ActiveSheet [ / B].UsedRange.Value = Sheets("Template").UsedRange.Value "

shows as red and I get a "Compile error: Syntax error" message.
Sorry, wrong tag. It should be:
Rich (BB code):
Sub CopySheet()
Dim i As Long, LastRow As Long, ws As Worksheet
Sheets("Cities").Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To LastRow
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.UsedRange.Value = Sheets("Template").UsedRange.Value
    ActiveSheet.Name = Sheets("Cities").Cells(i, 1)

Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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