Copy worksheet into newly created worksheets

mri81

New Member
Joined
Jan 1, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm trying to copy a template (from an existing worksheet "BOE_Template") into newly created worksheets from the code below. Basically i have a table, select my range, run it and new worksheets are created based on what is in each cell in that range. For example my table range might be "1.1, 1.2, 1.3 etc". New worksheets are created and the worksheets are renamed to 1.1, 1.2, 1.3). I'd like my template pasted into each of those newly created tabs before it loops back to make the next new worksheet. Hopefully this makes sense and I'm just learning VBA so any help would be appreciated.

Here is my existing code.

Sub Create_BOE_FROM_LIST()
Dim wks As Worksheet
row_ = Selection.Row
col_ = Selection.Column

For Each cell In Range(Cells(row_, col_), Cells(Selection.End(xlDown).Row, col_))
If cell <> "" Then
Set wks = Sheets.Add(after:=ActiveSheet)
wks.Name = cell.Value
End If

Next
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
See if the following gives you what you want - try it on a copy of your workbook. I'm assuming you know you can't include any of the following characters within valid sheet names:
/ \ [ ] * ? :

VBA Code:
Option Explicit
Sub Copy_Template()
    Dim wsTemp As Worksheet, c As Range
    Set wsTemp = Worksheets("BOE_Template")
    For Each c In Selection
        If c = "" Then
            MsgBox "Selection must contain valid sheet names"
            Exit Sub
        End If
        If Evaluate("ISREF('" & c & "'!A1)") Then
            MsgBox "Sheetname " & c & " already exists"
            Exit Sub
        End If
        wsTemp.Copy after:=ActiveSheet
        ActiveSheet.Name = c
    Next c
End Sub
 
Upvote 0
See if the following gives you what you want - try it on a copy of your workbook. I'm assuming you know you can't include any of the following characters within valid sheet names:
/ \ [ ] * ? :

VBA Code:
Option Explicit
Sub Copy_Template()
    Dim wsTemp As Worksheet, c As Range
    Set wsTemp = Worksheets("BOE_Template")
    For Each c In Selection
        If c = "" Then
            MsgBox "Selection must contain valid sheet names"
            Exit Sub
        End If
        If Evaluate("ISREF('" & c & "'!A1)") Then
            MsgBox "Sheetname " & c & " already exists"
            Exit Sub
        End If
        wsTemp.Copy after:=ActiveSheet
        ActiveSheet.Name = c
    Next c
End Sub
Thanks. I just got it working with additions to my original code below. I'll try your code too.

Sub Create_BOE_FROM_LIST()
Dim wks As Worksheet
Dim templatewks As Worksheet
row_ = Selection.Row
col_ = Selection.Column

Set templatewks = ThisWorkbook.Sheets("BOE Template")

For Each cell In Range(Cells(row_, col_), Cells(Selection.End(xlDown).Row, col_))
If cell <> "" Then
Set wks = Sheets.Add(After:=ActiveSheet)
wks.Name = cell.Value
templatewks.Cells.Copy Destination:=wks.Range("A1")

End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,360
Messages
6,184,508
Members
453,237
Latest member
lordleo

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