Search code examples
vbaexcel

Get Picture from Worksheet into Excel Userform


I am looking to view an image from the worksheet in an Image control on a userform.

This image will change based on the value on a combobox. I have inserted (Using: Insert -> Pictures) a couple of images into "Sheet1" of my workbook and named them "Picture1" & "Picture2".

I have created the below UserForm:

Form http://im56.gulfup.com/msKyqi.png

And this is the code that I am trying to use in order to load the images from the sheet, but unfortunately, this is not working at the moment.

Private Sub ComboBox1_Change()

UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))

End Sub

Private Sub UserForm_Initialize()

UserForm1.ComboBox1.Clear
UserForm1.ComboBox1.AddItem "Picture1"
UserForm1.ComboBox1.AddItem "Picture2"

UserForm1.ComboBox1.Value = "Picture1"

UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))

End Sub

Every time I run this code I get the below error:

Error http://im43.gulfup.com/YoWvTp.png

Please advise.


Solution

  • I figured it out!

    As I am using a UserForm there is a workaround to the issue.

    Instead of having the images in the worksheet to then try and load them in the form I tried having them in the UserForm in the first place, here is how.

    Create a frame on your userform: Frame http://im88.gulfup.com/Moy8I6.png

    Set the visible property of the frame to "False": Visible http://im88.gulfup.com/sAIQqh.png

    Insert your images by adding a picture control and loading the images, you can add as many images as you need: Images http://im88.gulfup.com/oas0EQ.png

    Name the images: Name http://im88.gulfup.com/cIO317.png

    Drag all the images one over the other into the frame, (you can then move the frame into a corner so it doesn't bother you:

    Drag http://im88.gulfup.com/1fOSut.png Move Away http://im88.gulfup.com/Q1fzKd.png

    Next create a picture control, this is what you will use to display the picture based on a selection:

    Form View http://im88.gulfup.com/X1UVRB.png

    In this example, I am going to use a combobox for the selection. Now insert the below code in to the form which is pretty straight forward:

        Private Sub ComboBox1_Change()
    
        ' Image1 is the name of the created picture control
        UserForm3.Controls.Item("Image1").Picture = UserForm3.Controls.Item(UserForm3.ComboBox1.Value).Picture 
    
        End Sub
    
        Private Sub UserForm_Initialize()
    
        UserForm3.ComboBox1.AddItem "Argentina"
        UserForm3.ComboBox1.AddItem "Brazil"
        UserForm3.ComboBox1.AddItem "Chile"
    
        End Sub
    

    As you will see, the frame with the pictures is Hidden, and the image is changing inside the picture control based on a selection:

    Result http://im88.gulfup.com/MSqyHF.png

    I think it's the better way to go as opposed to exporting the images from the worksheet to a Temp folder and then loading them back into the picture controls.