Show image based on Combobox selection

stirlingmw1

Board Regular
Joined
Jun 17, 2016
Messages
53
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
Platform
  1. Windows
Morning All
I have a Userform with Combobox which I select a ships name "HMS Bangor", "HMS Blyth", "HMS Brocklesby" etc (17 ships in total). I also have a stack of ships crest Images on the same Userform each named after the ship "HMS Bangor", "HMS Blyth", "HMS Brocklesby" etc. Is there an easier way of showing the selected crest and hiding the rest based on this combobox selection rather than "if this show that and then hide the rest", in effect trying to cover every permutation in an If statement?

Thanks

Steve
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I have a Image control on my Userform named Image1.
When the Userform opens I have a script that loads all the image names into a Listbox on my Userform.
In the listbox I have a script so if I select Duck the image named Duck is showed in the Image control.
So The actual image is not on my Userform it's still in my computers Image file. But when I choose duck from the Listbox the image named Duck is shown in my Image control
Here are my two scripts:
VBA Code:
Private Sub ListBox1_Click()
Me.Image1.Picture = LoadPicture("C:\Users\owner\Pictures\Ducks\" & ListBox1.Value)
End sub

This script loads all the image names into the listbox
VBA Code:
Private Sub UserForm_Initialize()
Filename = Dir("C:\Users\owner\Pictures\Ducks" & "\*.jpg", vbNormal)
Do While Len(Filename) > 0
UserForm1.ListBox1.AddItem Filename
Filename = Dir()
Loop
End Sub
 
Upvote 0
Thanks for your reply
Unfortunately having the picture files in a separate folder is not an option as the workbook I am developing will be sent out to my ships. If I add the pictures to a separate worksheet "Images" and name them in accordance with the Combobox selections how would I amend the code you have supplied.

Thanks

Steve
 
Upvote 0
I have a Image control on my Userform named Image1.
When the Userform opens I have a script that loads all the image names into a Listbox on my Userform.
In the listbox I have a script so if I select Duck the image named Duck is showed in the Image control.
So The actual image is not on my Userform it's still in my computers Image file. But when I choose duck from the Listbox the image named Duck is shown in my Image control
Here are my two scripts:
VBA Code:
Private Sub ListBox1_Click()
Me.Image1.Picture = LoadPicture("C:\Users\owner\Pictures\Ducks\" & ListBox1.Value)
End sub

This script loads all the image names into the listbox
VBA Code:
Private Sub UserForm_Initialize()
Filename = Dir("C:\Users\owner\Pictures\Ducks" & "\*.jpg", vbNormal)
Do While Len(Filename) > 0
UserForm1.ListBox1.AddItem Filename
Filename = Dir()
Loop
End Sub

Thanks for your reply
Unfortunately having the picture files in a separate folder is not an option as the workbook I am developing will be sent out to my ships. If I add the pictures to a separate worksheet "Images" and name them in accordance with the Combobox selections how would I amend the code you have supplied.

Thanks

Steve
 
Upvote 0
So let me see if I understand
You want to have a Worksheet in the active Workbook
With a sheet named Images
When the userform is opened all the images on this sheet will show up in the Combobox on the userform The Combobox is named Combobox1
Then if you choose "Alpha" the ship named "Alpha" will now appear in your Image control on your userform. Or if you choose "Bravo" the ship named "Bravo" will appear in the image control

Is this correct?
 
Upvote 0
So let me see if I understand
You want to have a Worksheet in the active Workbook
With a sheet named Images
When the userform is opened all the images on this sheet will show up in the Combobox on the userform The Combobox is named Combobox1
Then if you choose "Alpha" the ship named "Alpha" will now appear in your Image control on your userform. Or if you choose "Bravo" the ship named "Bravo" will appear in the image control

Is this correct?

Sorry, I am not being clear.

The Userform Combobox has the ships names (not pictures). If the text ships name "A" is selected from the Combobox Picture "A" is displayed on the Userform Image1 control.

As this Workbook will be utilised by all of my ships on their own platform and not from a central server, keeping the pictures in a Folder doesn't seem to be an option, so my thinking would be to have all of these pictures (Sized and labelled correctly) held within the active workbook "Images" worksheet.

I hope that helps clarify things.

Steve
 
Upvote 0
Yes I worded that wrong I said
When the userform is opened all the images on this sheet will show up in the Combobox on the userform
Should have said image name
 
Upvote 0
I'm sorry but I have not been able to come up with a way to do this.
I know how to load images from a file to the userform image control but not from a worksheet.
Maybe someone else here on the forum will have a answer.
 
Upvote 0
I'm sorry but I have not been able to come up with a way to do this.
I know how to load images from a file to the userform image control but not from a worksheet.
Maybe someone else here on the forum will have a answer.
thanks for taking the time to try.

regards

Steve
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,693
Members
449,048
Latest member
81jamesacct

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