0

I am new to VBA (I mean, REALLY new) and I would like you to give me some tips.

I have an Excel file with 2 columns: SKU and media_gallery I also have images stocked in a folder (lets name it /imageFolder)

I need to parse the imageFolder and look for ALL images sarting by SKU.jpg , and put them into the media_gallery column separated by a semicolon ( ; )

Example: My SKU is "1001", I need to parse the image folder for all images starting by 1001 (all image have this pattern: 1001-2.jpg , 1001-3.jpg etc...)

I can do that in Java or C# but I want to give a chance to VBA. :)

How can I do that?

EDIT: I only need file names yes! And I should of said that I have 20 000 images in my folder, and 8000 SKUs , so I don't know how we can handle looping on 20 000 images names.

EDIT2: If SKU contains a dash ( - ), I don't need to treat it, so I can pass to the next SKU. And each SKU has a maximum of 5 images (....;SKU-5.jpg)

Thanks all.

user1023021
  • 343
  • 4
  • 8
  • 13

1 Answers1

0

How to insert images given you have one image name per cell in a column: How to get images to appear in Excel given image url

Take the above and introduce an inner loop for the file name:

if instr(url_column.Cells(i).Value, "-") = 0 then

  dim cur_file_name as string
  cur_file_name = dir("imageFolder\" & url_column.Cells(i).Value & "*.jpg")

  do until len(cur_file_name) = 0
    image_column.Cells(i).Value = image_column.Cells(i).Value & cur_file_name & ";"      
    cur_file_name = Dir
  loop
end if
Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • I only need file names yes! And I should of said that I have 20 000 images in my folder, and 8000 SKUs , so I don't know how we can handle looping on 20 000 images names. EDIT2: If SKU contains a dash ( - ), I don't need to treat it, so I can pass to the next SKU – user1023021 Jun 12 '12 at 21:13
  • @user1023021 What do you mean, don't need to treat? And it's not looping on 20000 names, `Dir` applies a pattern, such as `SKU-5*.jpg`. This occurs on the file system level. – GSerg Jun 12 '12 at 21:18
  • if SKU contains a dash ( the SKU that we find in the SKU column) example: SKU = thisIsMy-SKU or SK-U or 111-5), I don't need to treat it. – user1023021 Jun 12 '12 at 21:31
  • @user1023021 Yes, but what do you mean by "not treat"? – GSerg Jun 12 '12 at 21:33
  • skip it, and do the next SKU in the column. Sorry for the misunderstanding – user1023021 Jun 13 '12 at 00:41
  • @user1023021 That's done with `instr`. I've put it into the answer. – GSerg Jun 13 '12 at 09:18