我目前正在尝试使用超链接自动链接两个用户选定的单元格。 用户可以选择两个不同的单元格(通过输入框一个接一个)。对于这两个单元格,应添加从小区1到单元格2和单元格2到单元格1的超链接。
到目前为止,我有这个:
Option Explicit
Sub Hyperlinks()
Dim FirstHyperlink As Range
Dim SecondHyperlink As Range
Set FirstHyperlink = Application.InputBox("Please select first cell to contain hyperlink", "Hyperlink 1 selection", Type:=8)
Set SecondHyperlink = Application.InputBox("Please select second cell to contain hyperlink", "Hyperlink 2 selection", Type:=8)
ActiveSheet.Hyperlinks.Add Anchor:=FirstHyperlink.Address, Address:="", SubAddress:= _
SecondHyperlink.Address, TextToDisplay:=FirstHyperlink.Value
ActiveSheet.Hyperlinks.Add Anchor:=SecondHyperlink.Address, Address:="", SubAddress:= _
FirstHyperlink.Address, TextToDisplay:=SecondHyperlink.Value
End Sub
但是我得到一个运行时错误13:类型不匹配指向
ActiveSheet.Hyperlinks.Add Anchor:=FirstHyperlink.Address, Address:="", SubAddress:= _
SecondHyperlink.Address, TextToDisplay:=FirstHyperlink.Value
任何人都知道导致问题的原因是什么?
答案 0 :(得分:2)
只需修正您的Anchors
:
Option Explicit
Sub Hyperlinks()
Dim FirstHyperlink As Range
Dim SecondHyperlink As Range
Set FirstHyperlink = Application.InputBox("Please select first cell to contain hyperlink", "Hyperlink 1 selection", Type:=8)
Set SecondHyperlink = Application.InputBox("Please select second cell to contain hyperlink", "Hyperlink 2 selection", Type:=8)
ActiveSheet.Hyperlinks.Add Anchor:=FirstHyperlink, Address:="", SubAddress:= _
SecondHyperlink.Address, TextToDisplay:=FirstHyperlink.Value
ActiveSheet.Hyperlinks.Add Anchor:=SecondHyperlink, Address:="", SubAddress:= _
FirstHyperlink.Address, TextToDisplay:=SecondHyperlink.Value
End Sub
这假设单元格开头有一些非空值。
答案 1 :(得分:1)
完整性原因:
这是我最终使用的代码。我写了一些代码行,使它可以在不同的工作表上工作,而不仅仅是在一个工作表上。
Option Explicit
Sub Hyperlinks()
Dim FirstHyperlink As Range
Dim SecondHyperlink As Range
Dim FirstSheet As Worksheet
Dim SecondSheet As Worksheet
Set FirstHyperlink = Application.InputBox("Please select first cell to contain hyperlink", "Hyperlink 1 selection", Type:=8)
Set FirstSheet = FirstHyperlink.Worksheet
Set SecondHyperlink = Application.InputBox("Please select second cell to contain hyperlink", "Hyperlink 2 selection", Type:=8)
Set SecondSheet = SecondHyperlink.Worksheet
ActiveSheet.Hyperlinks.Add Anchor:=FirstHyperlink, Address:="", SubAddress:= _
"'" & SecondSheet.Name & "'" & "!" & SecondHyperlink.Address, TextToDisplay:=FirstHyperlink.Value
ActiveSheet.Hyperlinks.Add Anchor:=SecondHyperlink, Address:="", SubAddress:= _
"'" & FirstSheet.Name & "'" & "!" & FirstHyperlink.Address, TextToDisplay:=SecondHyperlink.Value
End Sub