Image3.Picture = ThisWorkbook.Sheets("Sheet11").Shapes("Picture 2")

JNO

New Member
Joined
Feb 2, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I am having difficulty with the user form reference. What I am trying to do is populate an image box with a picture saved on the current worksheet.
Code is as follows:
Private Sub CommandButton8_Click()
displayPw = InputBox("Insert the password", "INSERT SIGNATURE")
If displayPw = "DT_MATT_1" Then
Image3.Picture = ThisWorkbook.Sheets("Sheet11").Shapes("Picture 1")
End If
If displayPw = "DT_KAYLA_2" Then
Image3.Picture = ThisWorkbook.Sheets("Sheet11").Shapes("Picture 2")
End If
End Sub

User form name is "COR1"
Worksheet name is "COR_DTL"
Thank You for any help you can provide

Image3.picture is not the proper reference
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Whoops let's start all over. I'll repost. The chart making shouldn't be in the CreateJpg sub. I'll repost soon. Dave
edit. Third times a charm...
Code:
Private Sub CommandButton8_Click()
On Error GoTo ErFix
Application.ScreenUpdating = False
'make chart and temp img files
With ThisWorkbook.Worksheets("sheet11").ChartObjects.Add(COR1.Image1.Left, _
               COR1.Image1.Top, COR1.Image1.Width, COR1.Image1.Height)
End With
Call CreateJPG("Picture 1", "DT_MATT_1")
Call CreateJPG("Picture 2", "DT_KAYLA_2")
'load userform Image1 control '***change image name to suit
displayPw = InputBox("Insert the password", "INSERT SIGNATURE")
If displayPw = "DT_MATT_1" Then
COR1.Image1.Picture = LoadPicture(Environ$("temp") & "" & "DT_MATT_1.jpg")
End If
If displayPw = "DT_KAYLA_2" Then
COR1.Image1.Picture = LoadPicture(Environ$("temp") & "" & "DT_KAYLA_2.jpg")
End If
'removce chart and temp files
ThisWorkbook.Worksheets("Sheet11").ChartObjects _
          (ThisWorkbook.Worksheets("Sheet11").ChartObjects.Count).Delete
Kill Environ$("temp") & "" & "DT_MATT_1.jpg"
Kill Environ$("temp") & "" & "DT_KAYLA_2.jpg"
ErFix:
If Err.Number <> 0 Then
On Error GoTo 0
MsgBox "Error"
End If
Application.ScreenUpdating = True
End Sub


Sub CreateJPG(PicName As String, FileNm As String)
Dim xRgPic As Shape
'create chart and image files
ThisWorkbook.Worksheets("sheet11").Activate
Set xRgPic = ThisWorkbook.Worksheets("sheet11").Shapes(PicName)
xRgPic.CopyPicture
With ThisWorkbook.Worksheets("sheet11").ChartObjects _
       (ThisWorkbook.Worksheets("sheet11").ChartObjects.Count)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "" & FileNm & ".jpg", "JPG"
End With
End Sub
 
Upvote 0
Just recently, a similar problem was solved like so:
On Sheet1:
Columns A, B and C have headers
Column A from 2nd row on down has names
Column B from 2nd row on down has values (password maybe) corresponding to the value in same row in Column A
Column C has pictures corresponding to values on same row in Columns A and B
It is important that the top left cell of the picture is withing the relative cell.
If the pictures are named the same as the values in Column A or B it will make things easier but the code needs to be changed accordingly.

In a regular module:
Code:
Sub Show_Me()
    UserForm1.Show
End Sub

The UserForm has
A ListBox named ListBox1
An Image control named Img1
A TextBox named TextBox2
A TextBox named TextBox1. This TextBox has an explanation on what to do. Not required.
A CommandButton named CommandButton1

Code in the UserForm module:
Code:
Private Sub CommandButton1_Click()
    Unload Me
End Sub

Code:
Private Sub ListBox1_Change()
Dim shp As Shape, sh1 As Worksheet, myPic As Shape, cntry As String
Dim tempChartObj As ChartObject
Dim strPath As String
Set sh1 = Worksheets("Sheet1")
strPath = ThisWorkbook.Path & "\Temp.jpg"

    For Each shp In sh1.Shapes
        If shp.TopLeftCell.Address = sh1.Columns(2).Find(ListBox1, , , 1).Offset(, 1).Address Then
            Set myPic = shp
                cntry = myPic.TopLeftCell.Offset(, -2).Value
            Exit For
        End If
    Next shp
   
    Set tempChartObj = sh1.ChartObjects.Add(100, 100, myPic.Width, myPic.Height)
        myPic.Copy
        DoEvents
            tempChartObj.Chart.ChartArea.Select
        DoEvents
            tempChartObj.Chart.Paste
                tempChartObj.Chart.Export strPath
            tempChartObj.Delete
        Me.Img1.Picture = LoadPicture(strPath)
Img1.PictureSizeMode = fmPictureSizeModeStretch
Me.TextBox2 = cntry
End Sub

Code:
Private Sub UserForm_Initialize()
Label1.Caption = Sheets("Sheet1").Range("B1").Value
ListBox1.List = Sheets("Sheet1").Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row).Value
End Sub

This is not to take away from NdNoviceHlp's work but maybe an alternative solution.

If you want a copy of the workbook you have to supply me with a throwaway email address
 
Upvote 0
For result of below code, see attached pictures.

Regular module:
Code:
Sub Show_Me_2()
    UserForm2.Show
End Sub

UserForm2 Module:
Code:
Private Sub CommandButton1_Click()
    Unload Me
End Sub

Code:
Private Sub ListBox2_Click()
Dim shp As Shape, sh1 As Worksheet, myPic As Shape, cntry As String
Dim tempChartObj As ChartObject
Dim strPath As String
Set sh1 = Worksheets("Sheet2")
strPath = ThisWorkbook.Path & "\TempA.jpg"
    Set myPic = Sheets("Sheet2").Shapes(Sheets("Sheet2").Columns(1).Find(ListBox2).Offset(, 1))
        Set tempChartObj = sh1.ChartObjects.Add(100, 100, myPic.Width, myPic.Height)
            myPic.Copy
            DoEvents
                tempChartObj.Chart.ChartArea.Select
            DoEvents
        tempChartObj.Chart.Paste
        tempChartObj.Chart.Export strPath
    tempChartObj.Delete
    Me.Img1.Picture = LoadPicture(strPath)
Img1.PictureSizeMode = fmPictureSizeModeStretch
End Sub

Code:
Private Sub UserForm_Initialize()
    Label1.Caption = "Select a " & Sheets("Sheet2").Range("A1").Value
    ListBox2.List = Sheets("Sheet2").Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value
End Sub


UserForm2 has a Label, ListBox, Image Control and a Command Button.
Sheet2 has names in Column A, the names of the pictures in Column B and Passwords in Column C, a Button and the Pictures (see attached pictures).
 

Attachments

  • Pic 1.JPG
    Pic 1.JPG
    44.1 KB · Views: 2
  • Pic 2.JPG
    Pic 2.JPG
    37.6 KB · Views: 2
  • Pic 3.JPG
    Pic 3.JPG
    41.2 KB · Views: 2
Upvote 0

Forum statistics

Threads
1,215,247
Messages
6,123,857
Members
449,129
Latest member
krishnamadison

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