通过Excel中的单元格向图像添加超链接

时间:2014-06-13 05:59:23

标签: excel vba excel-vba

有没有办法将从单元格提供的超链接添加到图像?

我尝试了以下VBA代码:

Sub Storage_Test_Click()
    ActiveSheet.Hyperlinks.Add Anchor:=storage_image, Address:=Worksheets("Links").Range("B8:B8").Value
End Sub 

但是使用这段代码,链接是持久的。换句话说,如果我更改单元格值,则图像中的链接不会受到影响。

谢谢, TRO

2 个答案:

答案 0 :(得分:1)

模块

中输入类似内容
Sub Add_HLink(ws As Worksheet, picture_name As String)
    Dim sh As Worksheet: Set sh = Worksheets("Link")
    Dim shp As Shape
    For Each shp In ws.Shapes
        If shp.Type = msoPicture And shp.Name = picture_name Then
            ws.Hyperlinks.Add shp, sh.Range("B8").Value
            Exit For
        End If
    Next
End Sub

然后在 链接表

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo halt
    Application.EnableEvents = False
    If Not Intersect(Target, [B8]) Is Nothing Then
        '~~> use the sub here, assuming Picture named "Picture 1" is in Sheet2
        Add_HLink Sheet2, "Picture 1" '~~> change the arguments to suit
    End If
continue:
    Application.EnableEvents = True

    Exit Sub
halt:
    MsgBox Err.Description
    Resume continue
End Sub

这是你正在尝试的吗? HTH。

答案 1 :(得分:0)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Hyperlinks.Add Anchor:=storage_image, Address:=Worksheets("Links").Range("B8:B8").Value
End Sub 

不要将宏指定给图像,因为你不能点击图像(因为它是一个超链接!),如果你在更新链接后用原始代码右键单击你的图像,我确定选择它和你的宏将运行,但上述更改应表示每次在工作表上进行选择时,您的链接都会更新