Home » excel » excel – Specifying a [Type] when adding a worksheet

excel – Specifying a [Type] when adding a worksheet

Posted by: admin May 14, 2020 Leave a comment


I need an Excel worksheet that has data in every row to run a macro that creates a sheet for every row and has each sheet be created with a custom template I have saved on my computer.

So far, I have the macro working to open each row in a sheet but I do not know how to get the sheet to open with the custom template. Also I’d like to rename every sheet corresponding to a specific cell in each sheet.

The current code I am using to create a new sheet from each row is:

Sub RowToSheet()
    Dim xRow As Long
    Dim I As Long
    With ActiveSheet
        xRow = .Range("A" & Rows.Count).End(xlUp).Row
        For I = 1 To xRow
            Worksheets.Add(, Sheets(Sheets.Count)).Name = "Row " & I
            .Rows(I).Copy Sheets("Row " & I).Range("A1")
        Next I
    End With
End Sub
How to&Answers:

There is an annoying issue with the Excel object model here. You can’t use Worksheets.Add and then use the Type parameter to specify the template. If you use Sheets.Add however, then it’ll work.

Try this:

Dim NewSheet As Worksheet
Set NewSheet = Sheets.Add(After:=Sheets(Sheets.Count), Type:=TemplatePath)
NewSheet.Name = "Row " & I.Rows(I)
NewSheet.Copy Sheets("Row " & I).Range("A1")

Set the TemplatePath variable to a suitable path.