简体   繁体   English

VBA Excel:在工作簿中添加带有模板的工作表

[英]VBA Excel: add sheet with template already in Workbook

Quick question: 快速提问:

I have a sheet TEMPLATE in my workbook. 我的工作簿中有一个TEMPLATE I want to add a number of similar sheet in that workbook, using TEMPLATE as a template. 我想使用TEMPLATE作为模板在该工作簿中添加许多相似的工作表。

How do I do this in VBA Excel? 如何在VBA Excel中做到这一点?

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

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. 修改它以满足您的需求。

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 可能不是代码可重用性的一个很好的例子,但是仍然可以选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM