Excel VBA : WorkSheet_FollowHyperlink is not getting called for Shape Object ( msoPicture )

牧云@^-^@ 提交于 2020-01-16 02:24:08

问题


I need to have a macro executed when someone clicks on the shape in the worksheet which is a picture. The macro intention is to display more information regarding the shape on which the user clicked. EDIT: I also needed an information display during mouse-rollover, hence used hyperlink method.

I followed the method outlined in the MSDN , but the macro just doesn't seem to run.

My Macro is as follows:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
  MsgBox "Source: Target.Range.Address  " & Target.Range.Address
  MsgBox "Source: Target.Range.Value " & Target.Range(1, 1).Value
  ' Some more macro stuff here
End Sub
  • On clicking the picture I get to the sheet where the hyperlink target is, but the macro doesn't get executed.
  • I created a dummy hyperlink on one of the cells (in the same sheet), if I click that one the macro is executed.
  • I used the hyperlink method to have the rollover information on the picture shape.

I am totally out of ideas about how to go about this. Help would be greatly appreciated.


回答1:


Don't use that method. Use Worksheet_SelectionChange instead of Worksheet_FollowHyperlink.

So change your code to

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    '~~> Change A1 to the cell which is behind the shape
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        '~~> Change this to the relevant sheet
        With ThisWorkbook.Sheets("Sheet1")
            .Visible = xlSheetVisible
            .Activate
        End With

        '
        '~~> Other Code if required
        '
    End If
End Sub

Next, Right Click on the Shape and click on Hyperlink and hyperlink to a cell behind the shape. You may also display information using the ScreenTip button in the Insert hyperlink dialog box.

We will do the sheet activating part in the above code rather than letting Excel do it.

Hope this solves your issue.



来源:https://stackoverflow.com/questions/22858643/excel-vba-worksheet-followhyperlink-is-not-getting-called-for-shape-object-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!