Excel VBA:超链接问题

时间:2017-09-08 14:57:29

标签: excel vba hyperlink

我在循环中有以下几行代码:

Worksheets(mainsheet).Cells(BidItem, "K").Value = "Test " & BidItem

Worksheets(mainsheet).Hyperlinks.Add Anchor:=Worksheets(mainsheet).Cells(BidItem, "L"), Address:="http://google.com", TextToAdd:="Test link"

执行时,

Dim mainsheet As String
mainsheet = "Tester"
BidItem = 6                 ' (in this loop, it then counts to 7 and 8)

结果是我将得到"测试6"在K6中,"测试7"在K7和"测试8"在K8 - 但在L6,L7,L8中没有任何东西。

我已经阅读了我在Hyperlinks.Add上找到的每个stackexchange和其他论坛,我无法弄清楚为什么这不起作用。它一定是Anchor的东西:=但我不知道是什么。我的错误在这里是什么?

2 个答案:

答案 0 :(得分:0)

尝试调试代码时,请不要使用On Error Resume Next

Dim bidItem As Long, mainsheet As String

mainsheet = "sheet3"

With Worksheets(mainsheet)
    For bidItem = 6 To 9
        .Cells(bidItem, "K").Value = "Test " & bidItem
        .Hyperlinks.Add Anchor:=.Cells(bidItem, "L"), Address:="http://google.com", TextToDisplay:="Test link"
    Next bidItem
End With

注意TextToDisplay而不是TextToAdd。这应该会引发错误。

答案 1 :(得分:0)

TextToAdd 不是 Hyperlinks.Add 方法的参数。请改用 TextToDisplay See the MS method documentation

Worksheets(mainsheet).Hyperlinks.Add Anchor:=Worksheets(mainsheet).Cells(BidItem, "L"), Address:="http://google.com", TextToDisplay:="Test link"