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
 
Loading the images from a sheet is not easy. It would be easier to add the images to the userform & then hide/unhide as needed.
You could then use something like
Rich (BB code):
Dim PicAry As Variant

Private Sub ComboBox1_Click()
   Call HidePics
   Me.Controls(Me.ComboBox1.Value).Visible = True
End Sub

Private Sub UserForm_Initialize()
   Dim i As Long
   
   PicAry = Array("Cricket", "Football", "Golf", "Rugby")
   Me.ComboBox1.List = PicAry
   Call HidePics
End Sub
Sub HidePics()
   Dim i As Long
   For i = 0 To UBound(PicAry)
      Me.Controls(PicAry(i)).Visible = False
   Next i
End Sub
The line in blue must go at the very top of the userform module, before any code
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Loading the images from a sheet is not easy. It would be easier to add the images to the userform & then hide/unhide as needed.
You could then use something like
Rich (BB code):
Dim PicAry As Variant

Private Sub ComboBox1_Click()
   Call HidePics
   Me.Controls(Me.ComboBox1.Value).Visible = True
End Sub

Private Sub UserForm_Initialize()
   Dim i As Long
  
   PicAry = Array("Cricket", "Football", "Golf", "Rugby")
   Me.ComboBox1.List = PicAry
   Call HidePics
End Sub
Sub HidePics()
   Dim i As Long
   For i = 0 To UBound(PicAry)
      Me.Controls(PicAry(i)).Visible = False
   Next i
End Sub
The line in blue must go at the very top of the userform module, before any code
Where in the script does it say the pictures are on a sheet named images?
 
Upvote 0
It doesn't, try reading what I said above the code.

@stirlingmw1
Forgot to mention that you will need to rename each image control to match the values in the combo.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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