VBA Excel:添加工作簿中已有模板的工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20380889/
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
VBA Excel: add sheet with template already in Workbook
提问by karamell
Quick question:
快速提问:
I have a sheet TEMPLATE
in my workbook. I want to add a number of similar sheet in that workbook, using TEMPLATE
as a template.
TEMPLATE
我的工作簿中有一张纸。我想在该工作簿中添加一些类似的工作表,TEMPLATE
用作模板。
How do I do this in VBA Excel?
如何在 VBA Excel 中执行此操作?
回答by Kazimierz Jawor
For 5 additional TEMPLATES
you need to copy it 5 times in the loop:
对于另外 5 个,TEMPLATES
您需要在循环中复制 5 次:
Dim i as byte
for i=1 to 5
Sheets("TEMPLATE").Copy after:=sheets("TEMPLATE")
Next i
回答by Gary's Student
Here is an example that makes 13 copies:
这是一个制作 13 个副本的示例:
Sub qwerty()
For i = 1 To 13
Sheets("TEMPLATE").Copy before:=Sheets(1)
Next i
End Sub
modify this to suit your needs.
修改它以满足您的需求。
回答by Tocchetto
Just to register here in case anyone stumble on this answer as I did, you can also do it with a range if you don't want to copy the whole sheet, for example if your "Template" is just a range from another sheet
只是为了在这里注册以防有人像我一样偶然发现这个答案,如果您不想复制整个工作表,您也可以使用一个范围来进行,例如,如果您的“模板”只是另一个工作表的一个范围
ThisWorkbook.Sheets("Master").Range("A2:L65536").Copy Destination:=ThisWorkbook.Sheets.Add(, Sheets("Data")).Range("A1")
What is important here:
这里重要的是:
Destination requires a range
目的地需要一个范围
Sheet.add returns a worksheet object.
Sheet.add 返回一个工作表对象。
so instead of passing sheetX.range as the Destination argument we are passing CreateNewSheetFunction.Range
所以我们传递的是 CreateNewSheetFunction.Range 而不是传递 sheetX.range 作为 Destination 参数
Might not be a good example of code redability, but still, it's an option
可能不是代码可红性的好例子,但它仍然是一个选择