问题
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