简体   繁体   中英

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.

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). 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.

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. If the flag is >= 2, run the code for picture 2 and set the flag to 3. And so on.

I have a slightly different suggestion.

  1. Name you pictures like "Pic01, Pic02, Pic03...Etc"
  2. Next Create a procedure in a module. Call it 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
  2. Next pass on the name of the image that you want to be active .

For example

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

This way whatever you set imgName , that image click will do something .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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