Modifying VBA Script to Add Pictures to Excel

amyHHD

New Member
Joined
Jul 15, 2019
Messages
3
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
Next
mainWorkBook.Save
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.:)

Amy
 

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
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
 
Upvote 0

Forum statistics

Threads
1,214,866
Messages
6,121,996
Members
449,060
Latest member
mtsheetz

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