How to Create a New Sheet from a Template Using a Macro in Excel – 3 Methods

 

Method 1 – Using a Button in a Template to Create a New Sheet

There is only one worksheet Sheet1 in the workbook.

Insert a command button:

excel macro create new sheet from template

 

Step 1: Inserting Command Button

  • Go to the Developer tab >> select Insert.
  • In ActiveX Controls >> select Command.

You will see a plus sign.

  • Right-click the plus sign to draw a box for the command button.

The Command Button is created.

excel macro create new sheet from template

Change the name of the Command Button.

  • Click the Command Button >> go to the Developer tab >> select Properties.

The Properties dialog box will be displayed.

  • In Caption, you can see the name of the Command button.

excel macro create new sheet from template

  • Rename it as Create New Sheet.
  • Close the Properties dialog box.

This is the output.

excel macro create new sheet from template

Step 2: Applying a Macro

  • Go to the Developer tab >> click Design Mode.
  • Double-click the command button.

The VBA editor window is displayed.

excel macro create new sheet from template

  • Enter the following code in the window.
Private Sub CommandButton1_Click()
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
End Sub

The Add method was used to add a worksheet to the existing worksheet.

  • Close the VBA editor window and go back to the worksheet.

Click the Create New Sheet command button. It will create a new sheet after Sheet1.

excel macro create new sheet from template

Sheet2 is created.
If you click the Create New Sheet button, another Sheet will be created.

Read More: Excel Macro to Create New Sheet and Copy Data


Method 2 – Using a Template with a Range of Cells to Create a New Sheet

There is a range of years in the Year column.

excel macro create new sheet from template

  • Go to the Developer tab >> select Visual Basic.

The VBA editor window will be displayed.
You can also press ALT+F11 to open the VBA editor window.

 

  • In Insert  >> select Module.

excel macro create new sheet from template

  • Enter the following code in the Module.
Sub create_new_sheet_from_template()

Dim year_range As Range
Dim yr_list As Range

On Error GoTo Errorhandling

Set year_range = Application.InputBox(Prompt:="Pick range of years:", _
Title:="create new sheet from template", _
Default:=Selection.Address, Type:=8)

For Each yr_list In year_range
  If yr_list <> "" Then
    Sheets.Add.Name = yr_list
  End If
Next yr_list

Errorhandling:

End Sub

Code Breakdown

  • create_new_sheet_from_template is the Sub.
  • year_range is declared as Range and yr_list as Range.
  • On Error GoTo Errorhandling handles errors if you select anything other than the range of years.
  • The For loop continues to create new sheets until it finds the last selected year.
  • The If statement is used to check whether the variable name is empty.
  • Add method is used to add names to the sheets.
  • Close the VBA editor window and go back to the worksheet.
  • Go to the Developer tab >> Select Macros in Code.

The Macro dialog box will be displayed.
You can press ALT+F8 to open the Macro dialog box,

excel macro create new sheet from template

The Macro name contains the Sub procedure name of the code.

  • Click Run.

The Create new sheet from template dialog box will be displayed.

  • Select B5:B7 in Pick range of years.
  • Click OK.

excel macro create new sheet from template

New sheets named 2021, 2020, and 2019  are created in the workbook.

Read More: How to Add Sheet with Name from Cell Using Excel VBA


Method 3 – Using a Button to Assign a Range of Cells Before Creating a New Sheet

There is a range of years in the Year column.

excel macro create new sheet from template

 


Step 1: Inserting Button

  • Go to the Developer tab >> select Insert.
  • In Form Controls >> select Button.

You can see Button1.

excel macro create new sheet from template

Step 2: Applying a Macro

  • Follow Method 2 to open a Module.
  • Enter the following code in the Module.
Sub create_new_sheet_from_template_1()
Dim year_range As range
Dim yr_list As range
On Error GoTo Errorhandling
Set yearrange = Application.InputBox(Prompt:="Pick range of years:", _
Title:="create new sheet from template", _
Default:=Selection.Address, Type:=8)
For Each yr_list In year_range
  If yr_list <> "" Then
  Sheets.Add.Name = yr_list
  End If
Next yr_list
Errorhandling:
End Sub

Code Breakdown

  • _new_sheet_from_template_1 is the Sub.
  •  year_range is declared as Range and yr_list as Range.
  • On Error GoTo Errorhandling handles error if you select anything other than the range of years.
  • The For loop continues to create new sheets until it finds the last selected year.
  • The IF statement is used to check whether the variable name is empty.
  • Add method is used to add names to the sheets.
  • Close the VBA editor window and go back to your worksheet.
  • Right-click Button1 >> select Assign Macro in the Context Menu.

excel macro create new sheet from template

The Assign Macro dialog box will be displayed.

  • Select Create_new_sheet-from_template_1 >> click OK.

  • Click Button 1.

excel macro create new sheet from template

The Create new sheet from template dialog box will be displayed.

  • Select B5:B8 in Pick range of years.
  • Click OK.

New sheets named 2022, 2021, 2020, and 2019 were created in the workbook.

excel macro create new sheet from template


Download Practice Workbook

Download the Excel file and practice.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Afia Kona
Afia Kona

Afia Aziz Kona, a graduate of Civil Engineering from Khulna University of Engineering & Technology, Bangladesh, serves as a technical content creator in the ExcelDemy project. Possessing a passion for innovation and critical thinking, she actively embraces challenges. Beyond her engineering background, Afia exhibits a keen interest in Excel, having authored numerous articles on Excel & VBA-related issues to simplify the experience for users facing obstacles in Excel. Apart from creating Excel tutorials, she is also... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo