Hey everyone,
I feel like I have a relatively simple challenge, but for the life of me can't figure out the solution myself.
I have often excel lists with product names in column A. I have two folders with several subfolders (and sub-subfolders) containing images of products. File names are identical to the product names i have.
I have a big macro that (among other things) searches through the two main folders, and if it finds image matching each product name - it inserts image next to it. if it doesn't find relevant image, the macro enters "no image found" on the line instead. So far so good.
My challenge is I can't get it to also look into the subfolders. Folder_location1 and Folder_location2 both have many subfolders, and even sub-subfolders.
I have in the macro defined the two main folders - "pic_folder" and "alt_pic_folder". Isn't there a way to make these two paths also include all their respective subfolders?
To keep the macro brief I've cut it down to what I believe is the relevant part for this question - the defining of "pic_folder" and "alt_pic_folder", and the way I've made the macro go through them to check for images.
Is my wish doable at all, or is some heavy macro modification required? Please help.
I feel like I have a relatively simple challenge, but for the life of me can't figure out the solution myself.
I have often excel lists with product names in column A. I have two folders with several subfolders (and sub-subfolders) containing images of products. File names are identical to the product names i have.
I have a big macro that (among other things) searches through the two main folders, and if it finds image matching each product name - it inserts image next to it. if it doesn't find relevant image, the macro enters "no image found" on the line instead. So far so good.
My challenge is I can't get it to also look into the subfolders. Folder_location1 and Folder_location2 both have many subfolders, and even sub-subfolders.
I have in the macro defined the two main folders - "pic_folder" and "alt_pic_folder". Isn't there a way to make these two paths also include all their respective subfolders?
To keep the macro brief I've cut it down to what I believe is the relevant part for this question - the defining of "pic_folder" and "alt_pic_folder", and the way I've made the macro go through them to check for images.
Is my wish doable at all, or is some heavy macro modification required? Please help.
VBA Code:
Sub select_file_add_pictures()
Dim lastCell, i As Long
Dim pic_folder$, alt_pic_folder$, fso, mypic As Object
Dim xPic As Picture
Dim Ws As Worksheet
Dim r As Long, Ppos As Long, m As Long
Dim shp As Shape
Dim Pic As Picture
user = Environ("username")
pic_folder = "C:\users\" & user & "\Folder_location1\"
alt_pic_folder = "C:\users\" & user & "\Folder_location2\"
'code to select the a file - removed to keep this short
Set Ws = ActiveSheet
'search for item photos
i = 1
lastCell = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For cell = 3 To lastCell Step 1
itemno = ActiveSheet.Range("A" & cell).Value
Filename = Dir(pic_folder$ & itemno & "." & "*")
If Filename = "" Then GoTo Line1 Else Filename = pic_folder & Filename 'extra line to check the file path - in case error is suspected
'ActiveSheet.Range("A" & cell).Offset(0, 1).Value = Filename
GoTo InsertPicture
Line1:
Filename = Dir(alt_pic_folder$ & itemno & "-" & "*" & "-" & "1." & "*")
If Filename = "" Then GoTo Line2 Else Filename = alt_pic_folder & Filename
'extra line to check the file path - in case error is suspected
GoTo InsertPicture
Line2:
ActiveSheet.Range("A" & cell).Offset(0, 1).Value = "No picture found"
GoTo Nextline
InsertPicture:
Set mypic = ActiveSheet.Shapes.AddPicture(fileName, False, True, 20, 20, -1, -1)
'insert-picture code here
Nextline:
i = i + 1
Next
End Sub