VBA Code To automatically insert pictuer in userform depending un combobox value

zlurba

New Member
Joined
Apr 18, 2017
Messages
3
Hello, would appreciate if someone could help me with this code. What i need is to automatically put a picture in userform after value in combobox is typed or selected. Combobox is in the same userform. The picture is with the same name as tha value. Both excel file and images files are in the same folder.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi zlurba,

So this is with a ComboBox and an image control in the userform

I've put the file path in the code and it assumes that all the picture file types are the same (i.e. jpeg's)

Code:
Private Sub ComboBox1_Change()
On Error GoTo Errhand

Me.Image1.Picture = LoadPicture("C:\Users\Alan_P\Desktop\Test Pics\" & ComboBox1.Value & ".JPG")
Exit Sub

Errhand:
MsgBox ("Picture not found, please check spelling or blah bla blah")

End Sub

Hope this helps,
Cheers,
Alan.
 
Upvote 0
Thanks very much!!! Works great!!

But i have one problem. If value in combobox is typed, can it be done that error message does not pop up after every letter is typed?
 
Upvote 0
Ah yes didn't think of that sorry :rolleyes:

Try this;

Code:
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
On Error GoTo Errhand

If KeyCode = vbKeyReturn Then
    
    Me.Image1.Picture = LoadPicture("C:\Users\Alan_P\Desktop\Test Pics\" & ComboBox1.Value & ".JPG")
    
End If

Exit Sub
Errhand:
MsgBox ("Picture not found, please check spelling or blah bla blah")
End Sub

So the picture will now only change once the enter key is pressed...

Cheers,
Alan.
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,890
Members
449,477
Latest member
panjongshing

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