使用 VBA 从外部工作簿添加工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8900413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a worksheet from an external workbook using VBA
提问by user793468
How do I use the Worksheet.Add
function to add a worksheet from an external workbook?
如何使用该Worksheet.Add
函数从外部工作簿添加工作表?
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`
对象“Sheets”的方法“Add”在第三行失败
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
回答by Siddharth Rout
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.
类型是可选变体。指定图纸类型。可以是以下 XlSheetType 常量之一:xlWorksheet、xlChart、xlExcel4MacroSheet 或 xlExcel4IntlMacroSheet。如果您基于现有模板插入工作表,请指定模板的路径。默认值为 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
xlt - Excel 2003 模板
xltx - Excel 2007/2010 Template
xltx - Excel 2007/2010 模板
xltm - Excel 2007/2010 Macro-Enabled Template
xltm - Excel 2007/2010 启用宏的模板