How to know when a picture is being selected in Excel with VBA? [closed]

孤人 提交于 2020-06-18 10:37:33

问题


I'm new to VBA (1 month), and I can't find how to get my code to know when I select a picture in Excel.

I want to be able to autoselect the cell containing the picture if I select the picture instead of the cell.

The picture already has the same name as the cell with "INV" as start (ex: INV$A$1).

The code must also work for double clicks, as double clicking the cell triggers some subroutine.

Everything is already written, but if I click the picture rather than the cell, nothing happens.


回答1:


Add a macro to your images when they're inserted. You can use the same macro for all images and check the value of Application.Caller to determine which image/shape was clicked.

Sub Pics_Clicks()
    ActiveSheet.Shapes(Application.Caller).TopLeftCell.Select
End Sub



回答2:


Try something like this:

Private Sub Image1_Click()
    MsgBox "clicked via Click!"
End Sub

Private Sub Image1_GotFocus()
    MsgBox "clicked via GotFocus!"
End Sub

Here, "Image1" is the automatically created name of a control of type Image. Such a control is inserted to an Excel sheet in Design Mode. Double click on the control to get the event handler routine auto-edited in the VBA editor.



来源:https://stackoverflow.com/questions/19717531/how-to-know-when-a-picture-is-being-selected-in-excel-with-vba

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