Home » excel » excel vba – Adding a worksheet from an external workbook using VBA

excel vba – Adding a worksheet from an external workbook using VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

How do I use the Worksheet.Add function to add a worksheet from an external workbook?

When I try to add a worksheet from an external workbook I get an error:

Method ‘Add’ of object ‘Sheets’ failed on the third line`

Here is my code:

Application.ScreenUpdating = False
Dim ws As Worksheet
Set ws = Worksheets.Add(After:=Sheet1, Count:=2,Type:="\SharedDrive\Worksheet\Student.xltm")
Application.ScreenUpdating = True
How to&Answers:

The “Type:=” has to be a template if you are inserting from another file.

The syntax is

expression.Add(Before, After, Count, Type)

Where

Type is Optional Variant. Specifies the sheet type. Can be one of the following XlSheetType constants: xlWorksheet, xlChart, xlExcel4MacroSheet, or xlExcel4IntlMacroSheet. If you are inserting a sheet based on an existing template, specify the path to the template. The default value is xlWorksheet.

For example

Set ws = Worksheets.Add(After:=Sheet1, Count:=2, _
Type:="\SharedDrive\Worksheet\Student.xlt")

or

Set ws = Worksheets.Add(After:=Sheet1, Count:=2,_
Type:="\SharedDrive\Worksheet\Student.xltm")

or

Set ws = Worksheets.Add(After:=Sheet1, Count:=2,_
Type:="\SharedDrive\Worksheet\Student.xltx")

xlt – Excel 2003 Template

xltx – Excel 2007/2010 Template

xltm – Excel 2007/2010 Macro-Enabled Template