将超链接添加到Excel VBA中的另一个选项卡会出现错误438

时间:2018-12-27 19:29:01

标签: excel-vba hyperlink runtime-error

我是VBA的新手,正在尝试使用这段VBA代码添加超链接:

Sub macrotoc()

Dim sht As Worksheet
Dim targetsheet As Worksheet

 Set sht = ActiveWorkbook.Sheets("TOC")
 Set targetsheet = Worksheets("Monthly Enrollment")

 With sht

 .Hyperlinks.Add Anchor:=sht.Range("c5"), Address:="", SubAddress:=targetsheet & "!A1", ScreenTip:="Monthly Enrollment", TextToDisplay:="Monthly Enrollment" <the line where code is giving me error>

 End With

End Sub

这是我在几个在线论坛的帮助下拼凑而成的。我希望超链接显示在工作表目录单元格C5中,并指向SAME工作簿中的另一个工作表“每月注册”。请注意,我将为多个工作簿超时运行此宏,因此我无法选择在超链接.add行中提供工作簿的位置。

代码现在给我运行时错误438:对象不支持Hyperlink.Add行上的此属性或方法

我一直在尝试在set sht =行中修改Activeworkbook或ThisWorkbook或Worksheets,但没有任何帮助。

您看到什么地方了吗?谢谢!

1 个答案:

答案 0 :(得分:0)

请改用此方法-您尝试将范围与字符串连接在一起,最好只是让它们全部成为字符串。

Sub macrotoc()

Dim sht As Worksheet
Dim targetsheet As String

 Set sht = ActiveWorkbook.Sheets("TOC")
 targetsheet = "Monthly Enrollment"

 With sht

 .Hyperlinks.Add Anchor:=.Range("C5"), Address:="", SubAddress:="'" & targetsheet & "'!A1", ScreenTip:="Monthly Enrollment", TextToDisplay:="Monthly Enrollment"

 End With

End Sub