I am trying to import pictures into Excel using VBA. My current code allows the user to select the folder in which the pictures are located and then it imports all of the pictures from that folder into excel. I would like to keep the folder selection process the same but only import selected photos. The selected photos would be determined by the photo name in a column of my spreadsheet. Below is my existing code. Any help would be greatly appreciated. I do not want to have to insert the file path into the code. I would like the user to select the folder the files are in and have the macro search that folder for the specific file names in column A and insert the corresponding pictures into excel.
Sub InsertPictures()
Dim myDialog As FileDialog, myFolder As String, myFile As String
Set myDialog = Application.FileDialog(msoFileDialogFolderPicker)
If myDialog.Show = -1 Then
myFolder = myDialog.SelectedItems(1) & Application.PathSeparator
myFile = Dir(myFolder)
Do While myFile <> ""
r = r + 2
With Cells(r, 14)
.RowHeight = 15
x = .Left
y = .Top
w = .Width
h = .Height
End With
myPicture = myFolder & myFile
ActiveSheet.Shapes.AddPicture (myPicture), _
msoFalse, msoTrue, x, y, w, h
myFile = Dir
Loop
End If
End Sub
Kyle Anderson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.