【问题标题】:Hyperlink to new workbook broken指向新工作簿的超链接已损坏
【发布时间】:2017-07-31 01:39:21
【问题描述】:

我是 Excel 新手,所以我希望这是有道理的。下面的代码显示了当单击用户表单上的按钮时,在特定工作簿(与当前工作簿不同)上创建的新工作表。不过,我在单独工作簿上创建的工作表的超链接似乎已损坏。我究竟做错了什么?有什么帮助,谢谢!

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

【问题讨论】:

  • 您在哪里/如何声明wsLastRow?链接是如何“断开”的?
  • 它只是将我的主页表上的超链接放在一个空白的单元格上。我不认为这是问题所在。它应该链接到单独工作簿上新创建的页面,但它没有。
  • 地址为空
  • 地址应该包含什么,就像我说我是 Excel 新手
  • LastRow = ws.Range("A" &amp; Rows.Count) 应该是 LastRow = ws.Range("A" &amp; ws.Rows.Count)... 以确保您计算 ws 上的行数。地址是超链接的地址 - 请参阅this page 了解更多信息。

标签: vba excel hyperlink excel-2010


【解决方案1】:

回答 01

此答案使用“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

此答案使用您最初想要的参考。我无法找到指向堆栈溢出问题的链接,但我测试了代码并且它可以工作。我喜欢这个选项,但它只在工作簿位于同一个 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

【讨论】:

  • 这很有趣。如果我没看错的话,您需要将名称放在一个字符串中,然后将该字符串添加到超链接中:string_name = .Name 然后在超链接语句中:SubAddress = "'" & string_name & "'!A1"。这将告诉我 .Name 是否引用了不同的工作表。这在测试用例中对我有用。如果它不起作用,请告诉我并发布您的代码,以便我查看。
  • 无效或不合格的引用错误 string_name = .Name
【解决方案2】:

有人帮我找到了合适的解决方案,所以这里是:

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

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多