简体   繁体   English

如何使用VBA将所选图像移到前面

[英]How to move selected image to front using VBA

Scenario: I have tons of images in a single worksheet,and many of them are overlapping with each other. 场景:我在一个工作表中有成千上万张图像,其中许多彼此重叠。

How can I write a VBA code so when user click on any image in that worksheet and Excel will automatically bring that image to the front? 我如何编写VBA代码,以便当用户单击该工作表中的任何图像时, Excel会自动将该图像显示在最前面?

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 is a solution... 但是一个解决方案...

You can make a single macro called Picture_click() , and assign that macro to each picture. 您可以创建一个名为Picture_click()宏,然后将该宏分配给每张图片。 There doesn't appear to be an event-driven way to achieve this, but the following works: 似乎没有一种事件驱动的方法可以实现此目的,但是可以进行以下工作:

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. 您只需要运行一次connect_all子项,即可将picture_click宏分配给每个形状。 Now, when you click a picture (or other shape) it comes to the front. 现在,当您单击图片(或其他形状)时,它位于前面。

Note - the Application.Caller tells you what object was clicked - in other words, what caused the call to picture_click . 注- Application.Caller告诉你被点击了什么对象-换句话说,是什么原因导致调用picture_click This is necessary because the "click" doesn't cause the picture to be selected yet - that happens after the macro has run. 这是必需的,因为“单击”尚未导致选择图片-在宏运行之后发生。 So you can't just use the selection. 因此,您不能只使用选择。 Tricky, that. 整rick的

I referenced this earlier post to 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 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. 这种方法有话要说。

With every shape, assign the BringToFront macro, then put the following in a module 对于每种形状,分配BringToFront宏,然后将以下内容放入模块中

Sub BrintToFront

Selection.ShapeRange.ZOrder msoBringToFront

end sub

Assign this macro to all Shapes: 将此宏分配给所有形状:

Sub lux()
    ActiveSheet.Shapes(Application.Caller).ZOrder msoBringToFront
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM