使用VBA创建新工作表副本

问题描述 投票:0回答:1

我目前正在编写一个代码,我在其中为每个使用命令按钮输入的“案例”创建一个新的工作表。但是,正在创建的工作表是空白的,我希望它们复制工作簿中的另一个工作表(我们称之为“模板”)。有没有人知道如何做到这一点?我会感激任何意见!

这是我的代码到目前为止(我已经在工作表之间添加了超链接):

financing = ws.Range("F2").Value
compName = ws.Range("F3").Value
fortnr = compName & "-" & financing

lastRow = ws.Cells(Rows.Count, "B").End(xlUp).Row + 1
ws.Cells(lastRow, "B") = financing
ws.Cells(lastRow, "C") = compName


ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveWorkbook.Sheets(Worksheets.Count).Name = compName & "-" & financing
ActiveWorkbook.Sheets(compName & "-" & financing).Visible = xlSheetVisible

ActiveSheet.Hyperlinks.Add Anchor:=ws.Cells(lastRow, 1), Address:="", SubAddress:= _
    "'" & fortnr & "'" & "!A1", TextToDisplay:="Check"                                  'Anchor: the place where the link will be

ActiveSheet.Hyperlinks.Add ActiveCell, "", Sheets("INPUT").Name & "!A1", TextToDisplay:="Back to Input-sheet"

End Sub
excel vba excel-vba
1个回答
3
投票

复制名为“Template”的整个工作表:

Sub Test()
    Worksheets("Template").Copy Before:=Worksheets("Template")
End Sub

将在“模板”左侧创建名为“Template(2)”的副本。或者使用After:=而不是Before:=将它放在右边。省略参数时,它将复制到新工作簿。

© www.soinside.com 2019 - 2024. All rights reserved.