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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Re: "folder basing on a drop down list at ref- U13"
What does the drop down list show? Full path and picture name with extension?
 
Upvote 0
Re: "folder basing on a drop down list at ref- U13"
What does the drop down list show? Full path and picture name with extension?
the picture are in the folder on desktop named PHOTO containing photos of students named according to their registration numbers
on the spreadsheet, students reg number is at U13 and the images is to be displayed at ref-P2
 
Upvote 0
Yes, I just noticed.
Normally I don't bother with code that does not work according to the OP.
Everybody has their own way of doing (solving) things.
 
Upvote 0
Check and change references if required.
Save attached picture into the PHOTO folder and name it NoPic.jpg
Code:
Sub Add_Pic()
If Dir("C:\PHOTO\" & Cells(13, 21).Value & ".jpg") <> "" Then
    ActiveSheet.Shapes.AddPicture("C:\PHOTO\" & Cells(13, 21).Value & ".jpg", False, True, Cells(2, 16).Left, Cells(2, 16).Top, 200, 300).Name = "Pic " & Cells(13, 21).Value
        Else
    ActiveSheet.Shapes.AddPicture("C:\PHOTO\NoPic.jpg", False, True, Cells(2, 16).Left, Cells(2, 16).Top, 200, 300).Name = "NoPic"
End If
End Sub
 

Attachments

  • NoPic.jpg
    NoPic.jpg
    2.2 KB · Views: 7
Upvote 0
Check and change references if required.
Save attached picture into the PHOTO folder and name it NoPic.jpg
Code:
Sub Add_Pic()
If Dir("C:\PHOTO\" & Cells(13, 21).Value & ".jpg") <> "" Then
    ActiveSheet.Shapes.AddPicture("C:\PHOTO\" & Cells(13, 21).Value & ".jpg", False, True, Cells(2, 16).Left, Cells(2, 16).Top, 200, 300).Name = "Pic " & Cells(13, 21).Value
        Else
    ActiveSheet.Shapes.AddPicture("C:\PHOTO\NoPic.jpg", False, True, Cells(2, 16).Left, Cells(2, 16).Top, 200, 300).Name = "NoPic"
End If
End Sub





Thank you so much.
how do i use this code?
because it not executing the required output
do i integrate it in my former code?
how do i interprete Cells(13,21)?
thank you
 
Upvote 0
Do you want it to work when selecting from the drop down?
It has nothing to do with your code. It replaces the code you showed in the first post.
Cells(13, 21) is the same as Range("U13"). The 13 is the Row number and the 21 is the column number.

Copy this into the module for the sheet where the drop down is. Change the "Sheet1" to your actual sheet name.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Cells(13, 21) Then Sheets("Sheet1").Pictures.Delete: Add_Pic
End Sub

In a regular module, paste the following code. Again, check all references and change as required.
Code:
Sub Add_Pic()
If Dir("C:\PHOTO\" & Cells(13, 21).Value & ".jpg") <> "" Then
ActiveSheet.Shapes.AddPicture("C:\PHOTO\" & Cells(13, 21).Value & ".jpg", False, True, Cells(2, 16).Left, Cells(2, 16).Top, 200, 300).Name = "Pic " & Cells(13, 21).Value
Else
ActiveSheet.Shapes.AddPicture("C:\PHOTO\NoPic.jpg", False, True, Cells(2, 16).Left, Cells(2, 16).Top, 200, 300).Name = "Pic " & "NoPic"
End If
End Sub

To paste code into a worksheet module:
Right mouse click on the sheet where the macro needs to work on. In your case, the sheet that has the drop down.
Select "View Code"
Delete the existing code or comment it out by putting an apostrophe in front of every line.
Paste the top code (the 3 lines macro) in the large white area on the right. On the left side your sheet in question will be highlighted.
At the top, click on "Insert" and "Module"
In the large white area paste the other (Add_Pic) macro.
Go back to your sheet with the drop down, safe your workbook and select from your drop down.
I assume you copied the "NoPic" picture into the PHOTO folder as mentioned previously.

 
Last edited:
Upvote 0
Change the worksheet_change macro to this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "U13" Then Sheets("Sheet1").Pictures.Delete: Add_Pic    '<---- Change sheet name as required and check cell reference
End Sub
 
Upvote 0
Better yet. Replace the code from Post #8 with this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("U13")) Is Nothing Then Sheets("Sheet1").Pictures.Delete: Add_Pic    '<---- Change sheet name to actual
End Sub
 
Upvote 0
Solution
thank you so much it has worked perfectly

however i did not get the option to define image dimensions
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
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