vba 如何确定哪个图像触发了宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14598079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine which image triggered a macro
提问by Jay Sullivan
In excel, I can assign a macro to an image, with this dialog:
在 excel 中,我可以使用此对话框为图像分配一个宏:
This results in the code:
这导致代码:
Private Sub MyShape_Click()
' ...
End Sub
However, I will need to assign this macro to many different images. How can I determine which image was selected from inside my click handler, and how do I get a reference to it?
但是,我需要将此宏分配给许多不同的图像。如何确定从单击处理程序中选择了哪个图像,以及如何获得对它的引用?
回答by peterm
UPDATE:
更新:
Assign the same macro to all your pictures and use Application.Caller
in your macro to determine the picture/shape that called it. In this case (picture/shape) Application.Caller
will be of type String
.
为所有图片分配相同的宏并Application.Caller
在宏中使用以确定调用它的图片/形状。在这种情况下(图片/形状)Application.Caller
将是 类型String
。
Private Sub GenericPicture_Click()
Select Case Application.Caller
Case "Picture 1"
'do your work
MsgBox "You got me"
Case "Picture 2"
'do your work
...
End Select
'Lets flip the picture that has been clicked
With ActiveSheet.Shapes(Application.Caller)
.Flip msoFlipHorizontal
End With
End Sub
And you might also implement required behavior calling a sub, which implements needed processing logic, with a parameter from separate macros:
您还可以实现调用子程序所需的行为,该子程序使用来自单独宏的参数实现所需的处理逻辑:
Sub Picture1_Click()
DoWork 1
End Sub
Sub Picture2_Click()
DoWork 2
End Sub
Sub DoWork(ByVal pic As Integer)
'Implement your logic here
MsgBox "Hi! I am picture" & pic
End Sub
Where Picture1_Click()
and Picture2_Click()
are macros that you assign to your images/shapes.
您分配给图像/形状的宏在哪里Picture1_Click()
和Picture2_Click()
在哪里。