简体   繁体   中英

new templated sheet to different workbook

In the code below, a button on a user form is creating a new sheet based off a template, and renaming it. Is there anyway to do this but have the new sheet created on a different workbook, and still have it accessed with the hyperlink? Anything helps. Thanks.

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
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

This should be everything you need. Also, I got rid of that useless loop.

   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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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