Thread: Excel template
View Single Post
 
Old 01-09-2025, 09:05 AM
Logit Logit is offline Windows 10 Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

Yes it can be done. Here is code that creates new sheets that conform to a TEMPLATE sheet located in the same workbook.

Code:
Sub MoreAndMoreSheets()

    Dim ListSh As Worksheet, BaseSh As Worksheet
    Dim NewSh As Worksheet
    Dim ListOfNames As Range, LRow As Long, Cell As Range

    With ThisWorkbook
        Set ListSh = .Sheets("List") '--Qualify our sheets.
        Set BaseSh = .Sheets("Template")
    End With

    LRow = ListSh.Cells(Rows.Count, "A").End(xlUp).Row '--Get last row of list.
    Set ListOfNames = ListSh.Range("A2:A" & LRow) '--Qualify our list.

    With Application
        .ScreenUpdating = False '--Turn off flicker.
        .Calculation = xlCalculationManual '--Turn off calculations.
    End With

    For Each Cell In ListOfNames '--For every name in list...
        BaseSh.Copy After:=Sheets(Sheets.Count) '--Copy Template sheet.
        Set NewSh = ActiveSheet '--Let's name it NewSh.
        With NewSh
            On Error GoTo messageinfo '--In case of errors.
            .Name = Cell.Value '--Set the sheet's name to that of our current name in list.
            GoTo LetUsContinue '--Skip to the LetUsContinue block.

LetUsContinue:
            On Error GoTo 0 '--Turn off error handling.
            .Range("C1") = Cell.Value '--Change C1 to the name of current sheet.
            .Calculate '--Calculate page.
            .Cells.Copy '--Change all cells...
            .Cells.PasteSpecial xlPasteValues '--... to values.
        End With
    Next Cell

    With Application
        .Calculation = xlCalculationAutomatic '--Return to automatic calculation.
        .ScreenUpdating = True '--Return to proper state.
        
    End With
Exit Sub
    

messageinfo: '--In case of duplicate names...
MsgBox ("Duplicate name exists")
Err.Clear

On Error GoTo 0
End Sub
The MAIN worksheet has a list of SHEET NAMES beginning in A1 and going down the same column. The TEMPLATE sheet is positioned as the first sheet in the workbook (not that it matters) and the MAIN sheet is in the second position.

When you run the macro it auto creates all of the sheets in the list based on the TEMPLATE sheet.

See attached.
Attached Files
File Type: xlsm Template Create Tabs.xlsm (21.3 KB, 4 views)
Reply With Quote