新的模板表到不同的工作簿

时间:2017-03-07 18:53:37

标签: excel vba excel-vba templates

在下面的代码中,用户表单上的按钮正在创建基于模板的新工作表,并重命名它。反正有没有这样做,但是在不同的工作簿上创建了新工作表,并且仍然可以通过超链接访问它?一切都有帮助。感谢。

Dim i as byte, sh as worksheet
for i=1 to 1
Sheets("TEMPLATE").Copy after:=sheets("TEMPLATE")
set sh = activeSheet
' Do whatever you have to do with the new sheet
sh.Name = AddEmployeeUF.txtFirstname.Text + AddEmployeeUF.txtMiddleinitial.Text + AddEmployeeUF.txtLastname.Text + "Template"
ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), Address:="", SubAddress:=sh.Name & "!A1", TextToDisplay:="View"
Next I

2 个答案:

答案 0 :(得分:0)

Dim i as byte, sh as worksheet
Dim newWB as Workbook
Dim thisWB as Workbook
Set thisWB = ThisWorkbook
set newWB = Application.Workbooks.Add
for i=1 to 1
    thisWB.Sheets("TEMPLATE").Copy after:=newWB.Sheets("Sheet1")
    set sh = newWB.Sheets("TEMPLATE")
    ' Do whatever you have to do with the new sheet
    sh.Name = AddEmployeeUF.txtFirstname.Text + _
              AddEmployeeUF.txtMiddleinitial.Text + _
              AddEmployeeUF.txtLastname.Text + "Template"
    ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), Address:="", _
                                       SubAddress:=sh.Name & "!A1", _
                                       TextToDisplay:="View"
Next I

答案 1 :(得分:0)

这应该是你需要的一切。而且,我摆脱了那个无用的循环。

   Dim newWB As Workbook
   Dim thisWB As Workbook
   Set thisWB = ThisWorkbook
   Set newWB = GetOrCreateWB("WBName", "C:\Users\...") '<--| Opening WB you want
   thisWB.Sheets("Template").Copy after:=newWB.Sheets(1)
   With ActiveSheet '<--| the just pasted worksheet becomes the active one
   .Name = AddEmployeeUF.txtFirstname.Text + AddEmployeeUF.txtMiddleinitial.Text + AddEmployeeUF.txtLastname.Text + "Template" '<--| Name it
   ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), Address:=newWB.Path & "\" & newWB.Name, SubAddress:="'" & .Name & "'!A1", TextToDisplay:="View" '<--| hyperlink to new sheet
相关问题