loading a picture from spreadsheet into userform

hearthhrob4gals

Board Regular
Joined
Aug 20, 2014
Messages
142
Hi,

I have few images in a sheet called 'Images'. Once the userform initializes, i want the 1st image (Picture 1) from the 'Images' sheet to be uploaded in the userform. I have written the below code. However getting error. Any help would eb appreciated

Code:
Worksheets("Images").Select
UserForm1.Picture = LoadPicture(ActiveSheet.Shapes("Picture 1"))
.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You can't do that directly you need to some code to paste the picture into a Chart object, save to file then recall that file.
Do you only need to show one picture or do you need to swap the pictures.???
 
Upvote 0
hi,

i have 3 pictures....initially on userform_initialize one needs to be uploaded.... then upon clicking options in a listbox, the userform picture should change..is that possible?
 
Upvote 0
hi,

i have 3 pictures....initially on userform_initialize one needs to be uploaded.... then upon clicking options in a listbox, the userform picture should change..is that possible?
 
Upvote 0
Your Userform should have an "Image Control" (Image1) and a "ListBox" (ListBox1).
The pictures are assumed to be on sheet2 (Change Sheet name in code as required,(2 places, sub "Pict", and "Initialize")
When you run the code the first image name in your listbox will be loaded.
After that it will change based on the Listbox selection.

Place the code below in the Userfrom Module:-
Code:
Option Explicit
Private Sub ListBox1_Click()
Call Pict(ListBox1.ListIndex)
End Sub


Private Sub UserForm_Initialize()
Dim Pic As Object
 For Each Pic In Sheets("Sheet2").Pictures
    If TypeName(Pic) = "Picture" Then
        ListBox1.AddItem Pic.Name
    End If
    Next Pic
ListBox1.ListIndex = 0
Call Pict(0)
End Sub


Sub Pict(n)
Dim Ans As String
Dim rng As Excel.Range
Dim cht As Excel.ChartObject
Dim Pth As String
Dim Pic As Object
With ListBox1
  Set Pic = Sheets("sheet2").Shapes(.List(n))
  Pic.Copy 'Picture xlScreen, xlBitmap
Dim Strpath As String
Strpath = ThisWorkbook.Path & "\Temp.jpg"
   Set cht = ActiveSheet.ChartObjects.Add(100, 0, Pic.Width, Pic.Height)
    cht.Chart.Paste
      cht.Chart.Export Strpath
        cht.Delete
Set cht = Nothing
Me.Image1.Picture = LoadPicture(Strpath)
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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