• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

pictures that become visible with button

Hi, I need to provide pictures for viewing that are only visible if the viewer clicks on a button saying "Sign 1", "Sign 2", "Sign 3", etc.. How would I write the code for that and would it be better to use insert a picture or screenshot? Pictures will be both jpegs and pdf - can you insert a pdf into excel? Thank you.
 
Hi, I need to provide pictures for viewing that are only visible if the viewer clicks on a button saying "Sign 1", "Sign 2", "Sign 3", etc.. How would I write the code for that and would it be better to use insert a picture or screenshot? Pictures will be both jpegs and pdf - can you insert a pdf into excel?

I should add that I do know how to add a button and assign a macro to it - it's writing the code to insert the pic or screenshot I'm struggling with. Thank you.
 
Hi Hank ,

See the attached file.

The picture on the left is a .jpg file which has been inserted into the worksheet ; the one on the right is a Bitmap Image Control , into which the same .jpg picture has been copied , pasted ; after pasting , the two have been grouped together.

Narayan
 

Attachments

  • Hank_Moore_Book1.xlsm
    74 KB · Views: 4
Hi Hank ,

See the attached file.

The picture on the left is a .jpg file which has been inserted into the worksheet ; the one on the right is a Bitmap Image Control , into which the same .jpg picture has been copied , pasted ; after pasting , the two have been grouped together.

Narayan
Great - thank you Narayank991... would you please explain what is being done here? What I don't understand is how is it being told which and where each picture(?) also, when I click on the button, the button goes away but not either picture. Please forgive my ignorance:)
 
Hi ,

Can you tell me how many buttons and how many pictures there will be in your file ? Depending on this , the code can be changed so that it becomes easier for you to customize it for your file.

Narayan
 
Hi Hank ,

See this file ; go through the comments at the beginning of the procedure , and I am sure you can customize it for your file.

Narayan
 

Attachments

  • Hank_Moore_Book1.xlsm
    72.3 KB · Views: 11
Sorry Narayan, I'm clueless on most of what you wrote...any chance you could explain it to me maybe line by line until I get it? I've never used DIM or pretty much anything in this code but don't think it'll take long to understand it.
I've create the matrix - do I need a name for it?
Thank you
 
Hi Hank ,

If you totally new to VBA , then I think the following links will help :

http://excelvbatutor.com/vba_tutorial.html

http://www.excel-vba.com/excel-vba-contents.htm

http://www.homeandlearn.org/

http://www.anthony-vba.kefra.com/vba/vbabasic1.htm

http://www.excel-pratique.com/en/vba.php

Any VBA procedure / macro has 2 sides to it ; the first and more important one is the logic or algorithm ; the step-by-step detailing of what is to be done to reach the final outcome. The second is the translation of this logic into a language which the computer understands. Going through the links given above should help you master the second step. I will detail the logic of the code.

The steps toward getting what you want are :

1. Detect which button has been clicked

2. Retrieve the picture which is linked to this button

3. Toggle the state of this picture i.e. if the picture was visible , hide it , and if it was hidden , display it.

The first step makes use of a VBA property called Application.Caller ; in our context this tells Excel which button was clicked , but there are other facets to this function , depending on the context in which it is executed. You can see the Excel help on this for more details.

In our code , what the Application.Caller returns is a string giving the name of the button which was clicked. I have named the first button Your button X , and so the Application.Caller property will return this , and this is being assigned to the VBA variable ButtonClicked.

Now , we need to retrieve the picture which is linked to this button.

More than any coding , what this involves is structuring the data so that this retrieval becomes easy. What I have done is set up a matrix called LinkingArray. This has 2 columns - the first column contains the names of the buttons viz. Your button X , Your button Y and Your button A. The second column contains the names of the associated pictures. In our case , the first button is linked to a picture which I have named Your first Picture.

If you want to customize this to your file , within the code , do a find and replace first ; search for Your button X , and replace all instances of this with the name of the first button in your file. Do the same for all the references to buttons and pictures.

The only other thing you need to do is change a constant I have named MAXBUTTONS ; within the code , this is set to 10 , which means you can have up to 10 buttons and 10 pictures. You need to change this only if you have more buttons / pictures than this.

The toggling of the state of the picture is by the line :

ActiveSheet.Shapes(LinkingArray(i, 2)).Visible = Not (ActiveSheet.Shapes(LinkingArray(i, 2)).Visible)

The Visible property of a shape can be either True or False ; if True , the shape is displayed ; if False , the shape is hidden.

This is what is known as a Boolean , and in this , the negation of one state is the other , which means Not True is the same as False , while Not False is the same as True.

Thus what we are telling Excel to do is take the current state of the shape and negate it , so that if it were displayed , after the execution of this statement , it would be hidden , and if it were hidden , then after the execution of the statement , it would be displayed.

I think if you go through the links given at the beginning of this post , you will understand the code in less time than it takes to go through this post !

Narayan
 
Back
Top