Help; create Macro to duplicate and rename tabs

endlessboundless

New Member
Joined
Jan 27, 2011
Messages
33
Hello,

Wondering if someone can help me out here. I have a tab that I want to duplicate based a list, and each new tab that is created, I would like it to be renamed based on this list. Could anyone help me out with a macro.

example Tab1 = Template

List
5210
5211
5212
ect

So a macro to duplicate "template" and create 3 tabs named 5210, 5211, and 5212. I would like the macro to be flexible so that if the list is larger, it will capture all.

TYIA
Amit
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try the following in a standard module

Code:
Sub newtabs()
    With Sheets("template")
        For Each t In .Range("A1", .Cells(Rows.Count, "A").End(xlUp))
            If Not sheetExists(t) and t <> "" Then Sheets.Add(after:=Sheets(Sheets.Count)).Name = t
        Next
    End With
End Sub

Function sheetExists(ByVal sname As String) As Boolean
    On Error Resume Next
    sheetExists = Sheets(sname).Range("A1").Cells.Count = 1
End Function
Make sure your sheet names are in column A in the 'template' tab and preferably begin in A1

HTH

PS the extra function makes sure there's no conflict with duplicate names.
 
Upvote 0
Hi,

The list is not in the template tab, its on a separate "input" tab. The template tab is what I want to duplicate based on the list.

Would this still work?

Thanks,
Amit
 
Upvote 0
I have this so far... but it gives me an error at "ActiveSheet.Name = cell" at the end.... highlighted in yellow. Also, it duplicates the cells I want but gives me one extra one. Any help? Thanks!

Function SheetExists(strSheetName As String) As Boolean
' returns TRUE if the sheet exists in the active workbook
SheetExists = False
On Error Resume Next
SheetExists = Len(Sheets(strSheetName).Name) > 0
On Error GoTo 0
End Function
Sub DuplicateRenameSheets()
Sheets("Input").Activate
For Each cell In Sheets("Input").Range("D17", Range("D65536").End(xlUp))
If Not SheetExists(cell.Value) Then
Sheets("template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = cell
End If

Sheets("input").Activate
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

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