迭代代码以使用excel vba添加超链接

时间:2017-03-14 12:55:36

标签: excel vba excel-vba

table_snpshot我有代码在一个单元格中创建超链接。我希望它沿着F或C或E列迭代所有填充的单元格。

Sub insertVeryLongHyperlink()

Dim curCell As Range
Dim longHyperlink, TextToDisplay1 As String

Set curCell = Range("G1")  ' or use any cell-reference
longHyperlink = [E1]
TextToDisplay1 = [C1]

curCell.Hyperlinks.Add Anchor:=curCell, _
                Address:=longHyperlink, _
                SubAddress:="", _
                ScreenTip:=" - Click here to follow the hyperlink", _
                TextToDisplay:=TextToDisplay1

End Sub

1 个答案:

答案 0 :(得分:2)

这会在column G中生成超链接。

Sub InsertVeryLongHyperlink()
    Dim cl As Range

    For Each cl In Range("G1:G" & Range("G1").End(xlDown).Row)
        cl.Hyperlinks.Add Anchor:=cl, Address:=cl.Offset(0, -2).Text, TextToDisplay:=cl.Offset(0, -4).Text, ScreenTip:=" - Click here to follow the hyperlink"
    Next cl
End Sub
  • cl.Offset(0, -2).Text是E栏
  • cl.Offset(0, -4).Text是C列