简体   繁体   English

Excel VBA 启用/禁用图片功能

[英]Excel VBA enable/disable functions on pictures

I have 10 pictures under itself where I assigned a Macro (which open another invisible specific sheet) on each of them.我下面有 10 张图片,我在其中为每张图片分配了一个宏(打开另一个不可见的特定工作表)。

First problem: When I open a Workbook, I just want to have active a first picture and the next 9 like non-able to active (click).第一个问题:当我打开工作簿时,我只想激活第一张图片和接下来的 9 张图片,例如无法激活(单击)。 So the user can click just to the first one and others are not active.所以用户可以只点击第一个,其他人不活跃。

Second problem: But when I/the user click on the first picture, then it open another specific sheet (already solved by第二个问题:但是当我/用户点击第一张图片时,它会打开另一个特定的工作表(已经解决了

Sheets("Example1").Visible = True
Sheets("Example1").Select

) and then it also activate possibility on the second picture to open it. ) 然后它还会激活第二张图片上的可能性以打开它。

The picture work like 'opening button' to other sheets, unfortunately, it must be a picture.图片就像其他工作表的“打开按钮”一样,不幸的是,它必须是图片。

Thank you very much for any help.非常感谢您的帮助。

Use a global variable or a cell on a hidden worksheet to store a flag value.使用全局变量或隐藏工作表上的单元格来存储标志值。

For example, when the workbook is opened, set the flag value to 1. When the first picture is clicked, set the flag to 2.例如,打开工作簿时,将标志值设置为 1。单击第一张图片时,将标志设置为 2。

When the second picture is clicked, first have code that checks the value of the flag.单击第二张图片时,首先要有检查标志值的代码。 If it is 1, exit the sub, so nothing happens.如果是 1,则退出 sub,所以什么也不会发生。 If the flag is >= 2, run the code for picture 2 and set the flag to 3. And so on.如果标志 >= 2,运行图 2 的代码并将标志设置为 3。依此类推。

I have a slightly different suggestion.我有一个稍微不同的建议。

  1. Name you pictures like "Pic01, Pic02, Pic03...Etc"将您的图片命名为“Pic01、Pic02、Pic03...等”
  2. Next Create a procedure in a module. Next 在模块中创建一个过程。 Call it Sub ExecuteImgClick将其Sub ExecuteImgClick

Paste this code粘贴此代码

Public imgName As String

Sub ExecuteImgClick()
    Dim img As String

    On Error Resume Next
    img = Application.Caller
    On Error GoTo 0

    If img = imgName And img = "Pic01" Then
        '~~> Do something
        imgName = "Pic02" '?????? Activate next pic
    ElseIf img = imgName And img = "Pic02" Then
        '~~> Do something
        imgName = "Pic03" '??????
    ElseIf img = imgName And img = "Pic03" Then
        '~~> Do something
        imgName = "Pic04" '??????
    ElseIf img = imgName And img = "Pic04" Then
        '~~> Do something
        imgName = "Pic05" '??????

        '
        ' And so on
        '
    End If
End Sub
  1. Assign Macro to all pictures by left clicking on the image and attach it to the above procedure通过左键单击图像Assign Macro给所有图片并将其附加到上述过程
  2. Next pass on the name of the image that you want to be active .接下来传递您想要active的图像的名称。

For example例如

Private Sub Workbook_Open()
    imgName = "Pic01"
End Sub

This way whatever you set imgName , that image click will do something .这样,无论您设置imgName什么,该图像单击都会do something

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

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