Modifying VBA Script to Add Pictures to Excel


New Member
Jul 15, 2019
Hello All,
I found a script online that I have successfully gotten to add pictures to my excel sheet in the location I want them. :) (Trust me this was an accomplishment I am new to scripting - read Please, please be patient with me and my bumbles here ;)) Here is that script, as I have it working.

Sub AddOlEObject()

Dim mainWorkBook As Workbook

Set mainWorkBook = ActiveWorkbook
Folderpath = "C:\Users\Graphics\Pictures\Test"
Set fso = CreateObject("Scripting.FileSystemObject")
NoOfFiles = fso.GetFolder(Folderpath).Files.Count
Set listfiles = fso.GetFolder(Folderpath).Files
For Each fls In listfiles
strCompFilePath = Folderpath & "" & Trim(fls.Name)
If strCompFilePath <> "" Then
If (InStr(1, strCompFilePath, "jpg", vbTextCompare) > 1 _
Or InStr(1, strCompFilePath, "jpeg", vbTextCompare) > 1 _
Or InStr(1, strCompFilePath, "png", vbTextCompare) > 1) Then
counter = counter + 1
ActiveSheet.Range("C" & counter).Value = fls.Name
ActiveSheet.Range("D" & counter).ColumnWidth = 10
ActiveSheet.Range("D" & counter).RowHeight = 60
ActiveSheet.Range("D" & counter).Activate
Call insert(strCompFilePath, counter)
End If
End If
End Sub

Function insert(PicPath, counter)

With ActiveSheet.Pictures.insert(PicPath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 60
.Height = 60
End With
.Left = ActiveSheet.Range("D" & counter).Left
.Top = ActiveSheet.Range("D" & counter).Top
.Placement = 1
.PrintObject = True
End With
End Function

Now, what it is doing is getting the images from a regular folder on my hard drive. And it gets ALL of the pictures in the folder.

My project that I am trying to modify this for consists of HUNDREDS of pictures that are in a folder of THOUSANDS of pictures. I only need it to get the hundreds of pictures.

Is there a way to have the script look at a column in my spreadsheet and match the information in a cell to a picture filename in the folder? And then put that picture in the cell a couple of columns over on the same row?

My data is part numbers. The pictures are of these parts and they are named by their part numbers with an extension of .jpg

I have the Part Number in column A, the Description in column B and I would ultimately like the corresponding picture in column C.

Right now the script will put the picture name in column C and the picture in column D (which is fine, I can just delete column C eventually) but it gets ALL the pictures in the folder AND they do not match the part numbers in column A.

Thank you for any help in advance.:)



New Member
Jul 15, 2019
Good Afternoon,

Feeling a little discouraged here. Is my question impossible or did I do I bad job on my description or do I need to wait a little longer? This was my first ever post. Open to any feedback please.

Thank you so much

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics