超链接到新的工作簿中断

我是新来的Excel,所以我希望这是有道理的。 下面的代码显示了在单击用户表单上的button时,在特定工作簿(与当前工作簿分开)上创build的新工作表。 虽然,我在单独的工作簿上创build的工作表的超链接似乎已被打破。 我究竟做错了什么? 任何帮助,谢谢!

Dim LastRow As Long, ws As Worksheet Set ws = Sheets("Employee Information") LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 If Me.cbStores.Value = "Northern" Then Dim newWB As Workbook Dim thisWB As Workbook Set thisWB = ThisWorkbook Set newWB = GetOrCreateWB("EmployeeTemplates", "C:\Users\...\Folder") '<--| Opening EmployeeTemplates wb 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:="", SubAddress:=.Name & "!A1", TextToDisplay:="View" '<--| hyperlink to new sheet End With End If 

答案01

此答案使用文件path,如“C:\ Users \ Me \ Desktop \ newfile.xlsx”

 With ActiveSheet '<--| the just pasted worksheet becomes the active one .Name = AddEmployeeUF.txtFirstname.Text + _ AddEmployeeUF.txtMiddleinitial.Text + _ AddEmployeeUF.txtLastname.Text + "Template" '<--| Name it ' the hyperlink SubAddress needs a valid file path or hyperlink to ' work like "C:\User\me\Desktop\newfile.xlsx" ' .Name & "!A1" references a cell not the file location on the computer ' or network 'ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _ ' Address:="", SubAddress:=.Name & "!A1", _ ' TextToDisplay:="View" '<--| hyperlink to new sheet ' you need something like this ' as long as newWB.Path property is set you should be good ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _ Address:="", SubAddress:=newWB.Path, _ TextToDisplay:="View" '<--| hyperlink to new sheet End With 

答案02

这个答案使用了一个你最初想要的参考。 我很难find堆栈溢出问题的链接,但我testing了代码,它的工作原理。 我喜欢这个选项,但是只要工作簿在同一个Excel应用程序中就可以工作。 如果您打开了两个应用程序,它将不起作用,因为在应用程序中没有对新工作簿的引用。

 With ActiveSheet '<--| the just pasted worksheet becomes the active one .Name = AddEmployeeUF.txtFirstname.Text + _ AddEmployeeUF.txtMiddleinitial.Text + _ AddEmployeeUF.txtLastname.Text + "Template" '<--| Name it ' the hyperlink SubAddress needs a valid file path or hyperlink to ' work like "C:\User\me\Desktop\newfile.xlsx" ' .Name & "!A1" references a cell not the file location on the computer ' or network 'ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _ ' Address:="", SubAddress:=.Name & "!A1", _ ' TextToDisplay:="View" '<--| hyperlink to new sheet ' you need something like this ' as long as newWB.Path property is set you should be good ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), _ Address:="", SubAddress:="'" & .Name & "'!A1", _ TextToDisplay:="View" '<--| hyperlink to new sheet End With 

有人帮我find了一个合适的解决scheme,所以这里是:

 ws.Hyperlinks.Add Anchor:=ws.Range("F" & LastRow), Address:=newWB.Path & "\" & newWB.Name, SubAddress:="'" & .Name & "'!A1", TextToDisplay:="View" '<--| hyperlink to new sheet