vba 如何使用VBA将选定的图像移到前面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24043739/
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 move selected image to front using VBA
提问by user3708103
Scenario: I have tons of images in a single worksheet,and many of them are overlapping with each other.
场景:我在一个工作表中有大量图像,其中许多图像相互重叠。
How can I write a VBAcode so when user click on any image in that worksheet and Excelwill automatically bring that image to the front?
如何编写VBA代码,以便当用户单击该工作表中的任何图像时,Excel会自动将该图像置于最前面?
回答by Floris
If you have an object myObject
that you want to bring to the front you use
如果你myObject
想把一个物体带到前面,你可以使用
myObject.ShapeRange.Zorder msoBringToFront
http://msdn.microsoft.com/en-us/library/office/aa213680(v=office.11).aspx
http://msdn.microsoft.com/en-us/library/office/aa213680(v=office.11).aspx
Your object might for example be the selection:
例如,您的对象可能是选择:
set myObject = Selection
As for responding to a click - that's trickier. There doesn't appear to be an event for "selecting a picture" . But there isa solution...
至于响应点击 - 这更棘手。似乎没有“选择图片”的事件。但有是一个解决方案...
You can make a single macro called Picture_click()
, and assign that macro to each picture. There doesn't appear to be an event-driven way to achieve this, but the following works:
您可以创建一个名为 的宏Picture_click()
,并将该宏分配给每张图片。似乎没有事件驱动的方式来实现这一点,但以下工作:
Add the following to a module:
将以下内容添加到模块中:
Sub picture_click()
ActiveSheet.Shapes(Application.Caller).Select
Selection.ShapeRange.ZOrder (msoBringToFront)
End Sub
Sub connect_all()
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
sh.OnAction = "picture_click"
Next sh
End Sub
You have to run the connect_all
sub just once, to assign the picture_click
macro to each shape. Now, when you click a picture (or other shape) it comes to the front.
您connect_all
只需运行一次子程序,即可将picture_click
宏分配给每个形状。现在,当您单击图片(或其他形状)时,它会出现在前面。
Note - the Application.Caller
tells you what object was clicked - in other words, what caused the call to picture_click
. This is necessary because the "click" doesn't cause the picture to be selected yet - that happens afterthe macro has run. So you can't just use the selection. Tricky, that.
注意 -Application.Caller
告诉您点击了什么对象 - 换句话说,是什么导致了对picture_click
. 这是必要的,因为“单击”不会导致图片被选中 - 这发生在宏运行之后。所以你不能只使用选择。棘手,那个。
I referenced this earlier postto come up with this solution - specifically, RichJ's solution.
我参考了这篇较早的帖子来提出这个解决方案 - 特别是 RichJ 的解决方案。
afterthought
事后的想法
If you don't want the picture to be selected after you clicked it, you can modify the picture_click()
sub to the following:
如果不想图片点击后被选中,可以将picture_click()
sub修改为如下:
Sub picture_click()
ActiveSheet.Shapes(Application.Caller).ZOrder msoBringToFront
End Sub
This will move the picture to the front but doesn't select it. There is something to be said for that approach.
这会将图片移到前面,但不会选择它。对于这种方法,有些话要说。
回答by SilverShotBee
With every shape, assign the BringToFront
macro, then put the following in a module
对于每个形状,分配BringToFront
宏,然后将以下内容放入模块中
Sub BrintToFront
Selection.ShapeRange.ZOrder msoBringToFront
end sub
回答by Gary's Student
Assign this macro to all Shapes:
将此宏分配给所有形状:
Sub lux()
ActiveSheet.Shapes(Application.Caller).ZOrder msoBringToFront
End Sub