Search code examples
excelvbaexcel-2007

Want to create new sheets that copy a template in a specific order based on a list of names on a sheet


I wanted to create new sheets based on a list and have all those new sheets copy a template on a currently existing sheet. I've tried looking for examples but all the ones i find only create a list of sheets, or just copy the template. I know im close but I can't seem to get this to work.

This is what i have so far, and it works, but when the sheets are created they show up as client3, client2, client1. I want it to show up in the reverse order. I know i need to change the copy after statement but every time I try I get an error. Any help would be appreciated Sheets names i have are Clients and Scan

Option Explicit
Sub NewSheets()
Dim i As Integer
Dim ws As Worksheet
Dim sh As Worksheet
Dim sh2 As Worksheet
Set ws = Sheets("Scan")
Set sh = Sheets("Clients")
Set sh2 = Sheets("Scan")
Application.ScreenUpdating = 0

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
        Sheets("Scan").Copy After:=sh2
        ActiveSheet.Name = sh.Range("A" & i).Value
Next i
End Sub

Solution

  • How about something like below:

    Option Explicit
    Sub NewSheets()
    Dim ws As Worksheet: Set ws = Sheets("Scan")
    Dim sh As Worksheet: Set sh = Sheets("Clients")
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    For i = 2 To sh.Range("A" & sh.Rows.Count).End(xlUp).Row
        Sheets("Scan").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        ActiveSheet.Name = sh.Range("A" & i).Value
    Next i
    
    Application.ScreenUpdating = True
    End Sub