How to insert jpg images into spreadsheet using VBA

EMASOFT

New Member
Joined
Jun 24, 2024
Messages
10
Office Version
  1. 2021
thank you for your prevoius codes
i wanted a vba code that picks a photo from folder basing on a drop down list at ref- U13 and display it at ref P2 and if the drop option has no image in the folder, a blank image be displayed for that option without returning any error message
the code i am having is

VBA Code:
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:\PHOTO\" & Range("U13").Text & ".jpg"

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

myPict.Height = 300
myPict.Width = 200
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
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
the picture that displays after running my codes removes all other pictures in the spreadsheet
how do i disable that? thank you
 
Upvote 0
From Post #7: "Cells(2, 16).Left, Cells(2, 16).Top, 200, 300"
The 200 and 300 are the width and height.

From Post #9: "Sheets("Sheet1").Pictures.Delete:"
Remove this part to not delete all pictures.
You had that in your first post so I thought you wanted it.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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