Show/hide picture depending on value in a combobox

ulrika

New Member
Joined
Jan 9, 2011
Messages
2
Hi everyone,

I hope you can help me since I'm really struggling to show and hide pictures in my userform, depending on the value in a combobox.

My combobox is called comboItem and I want to show the picture imgOwls as default, UNLESS the value in combobox comboItem is "Blocks". If the value in comboItem is "Blocks", then I want to show a picture of wooden blocks which I have named imgBlocks. The idea is to preview the item in the userform before adding it to my database. The picture of the Owls is my default picture which should be shown when no value is selected in the combobox.

I have tried the following VBA code;
If comboItem.Value = "Blocks" Then Me.imgBlocks.Visible = True
If comboItem.Value = "Blocks" Then Me.imgOwls.Visible = False<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
If comboItem.Value <> "Blocks" Then Me.imgBlocks.Visible = False<o:p></o:p>
If comboItem.Value <> "Blocks" Then Me.imgOwls.Visible = True<o:p></o:p>
The code above works to some degree; I just can't get rid of the imgBlocks picture and get back to the default picture (imgOwls).


I have also tried this code:

If comboItem.Value = "Blocks" Then
Me.imgOwls.Visible = False

Me.imgBlocks.Visible = True
Else
Me.imgOwls.Visible = True
End If


I have chosen Visible = True for imgOwls in the properties menu, while I have chosen Visible = False for imgBlocks.




I would really appreciate your input to resolve this frustrating problem. :confused:

Thanks in advance!

Best regards
Ulrika
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
hi ulrike and welcome to the board.
just checking: are both the images in seperate image controls?
 
Upvote 0
also, what items are there in the combobox, and what order are they in
 
Upvote 0
Thanks for the warm welcoming, Diddi.

Yes, that is right. Both pictures are in two separate image controls.


I have only text alternatives in my combobox (comboItem). The three values are;
"Duck"
"Bear"
"Blocks"

My intention is to show a picture of a Duck when the value "Duck" is selected in the combobox. Furthermore, will a picture of a bear be shown when the "Bear"-value has been chosen. When none of the values have been chosen, I want my default owl picture. But first things first. I have to understand the hide/show conditions much better....

I appreciate your rapid assistance. :beerchug:

Ps. I agree; indented code is much better and friendlier to the eye. :)
 
Upvote 0
i think ide try this:

Code:
Sub combobox1_change
    select case combobox1.listindex
        case 0
             imgDuck.Visible = True
             imgBear.Visible = False
             imgBlocks.Visible = False
        case 1
             imgDuck.Visible = False
             imgBear.Visible = True
             imgBlocks.Visible = False
        case 2
             imgDuck.Visible = False
             imgBear.Visible = False
             imgBlocks.Visible = True
    end select
end sub
 
Last edited:
Upvote 0
Since your requirements seem to be fairly basic...
For an alternative non-VBA solution, try this:

Assumption: Pictures are stored on Sheet2 and will be dynamically shown on Sheet1.

Select Sheet2 and turn off Grid Lines
...Tools.Options.View_tab...Uncheck: Grid Lines

1)For each picture to be displayed:
1a. Insert.Picture_from_file.. (select picture and put it in the sheet).
1b. Select the range of cells that contains the picture.
1c. Name that range of cells, using the prefix "pic" followed by the dropdown list text:

Example for a picture of an Elephant:
Insert.Name.Define
Names in Workbook: picElephant
Refers to: (the range of cells under the picture)

2)Build your data validation list on a cell in Sheet1 and pick one of the items.

3)Create a dynamic range name that refers to that cell:
Insert.Name.Define
Names in Workbook: ShowMyPic
RefersTo: =INDIRECT("pic"&Sheet1!$A$1)
...or whatever cell you chose.

4)Copy/Paste one of the pictures from Sheet2 to the display cell on Sheet1.

5)With the picture selected, type this in the formula bar, then press [Enter]:
=ShowMyPic

The picture will be replaced by the picture referred to by the dropdown list.

Each time you select a different item in the list, the associated picture will appear in the picture box and resize appropriately.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top