insert picture by VBA

EMASOFT

New Member
Joined
Jun 24, 2024
Messages
10
Office Version
  1. 2021
greetings
i have a spread sheet displaying photos on a square shape basing on cell contents on a drop down cell value at A1
my desire is to have all photos fitting into the size of the shape and if a name from A1 does not have a corresponding photo in the folder, then a blank image should display for that particular name
however part of my code is






Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False

Dim myPict As Picture
Dim PictureLoc As String

If Target.Address = Range("U13").Address Then

ActiveSheet.Pictures.Delete

PictureLoc = "C:\Users\EMASOFT\Desktop\PHOTO\" & Range("A1").Text & ".jpg"

With Range("P2")
On Error GoTo errormessage:
Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)

myPict.Height = 100
myPict.Width = 100
myPict.Top = .Top
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
myPict.ShapeRange.LockAspectRatio = msoTrue

errormessage:
If Err.Number = 1004 Then

MsgBox "File does not Exist, Please first update photo with .jpg File"

End If
End With
End If
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to Mr. Excel.
Possibly something like this.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    
    Dim myPict As Picture
    Dim PictureLoc As String, BlankPictureLoc As String
    
    If Target.Address = Range("U13").Address Then
        ActiveSheet.Pictures.Delete
        
        PictureLoc = "C:\Users\EMASOFT\Desktop\PHOTO\" & Range("A1").Text & ".jpg"
        BlankPictureLoc = "C:\Users\EMASOFT\Desktop\PHOTO\BlanksPicture.jpg"          '<--------------- Blank image file. You must edit!
        
        With CreateObject("Scripting.FileSystemObject")
            If Not .FileExists(PictureLoc) Then
                MsgBox "File does not Exist, Using blank image"
                PictureLoc = BlankPictureLoc
                
                If .FileExists(PictureLoc) Then
                    With Range("P2")
                        Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
                        myPict.Height = 100
                        myPict.Width = 100
                        myPict.Top = .Top
                        myPict.Left = .Left
                        myPict.Placement = xlMoveAndSize
                        myPict.ShapeRange.LockAspectRatio = msoTrue
                        Application.ScreenUpdating = True
                    End With
                End If
            Else
                MsgBox "File '" & PictureLoc & "' does not exist", vbCritical, "Blank Image Failure"
            End If
        End With
    End If
                    
  Application.ScreenUpdating = True
End Sub


(Tip: For future posts , please try to use code tags like I did above when posting code. It makes your code easier to read and copy.
)
 
Upvote 0

Forum statistics

Threads
1,224,915
Messages
6,181,726
Members
453,064
Latest member
robatthe2A

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