I need to evolve the following code to concatenate data in a certain way. I have a folder with product pictures (several photos for each product) and I want Excel to search the name of the photos and write the names of the photos related to each product in a cell and separated by a comma.
Another issue is that for each product photos can have different extensions ie you can have JPG and / or PNG and / or JPEG >. (see examples)
The name of the photos is the same as the reference of the product and if the product has more than one photo the name is differentiated by a letter of the alphabet at the end of the name and before the point and the extension. Ex: ac2345 a .jpg or 023198AA b .jpg or GDV7YDX c .jpeg The name can be just numbers, or just letters or a mix of numbers and letters.
Another condition is that in order to exist an image ac2345 a .jpg there must be an ac2345.jpg (or png or jpeg) image to exist a GDV7YDX c .jpeg must have a GDV7YDX.jpg (or png or jpeg) image, a GDV7YDX a .jpg (or png or jpeg) image and a GDV7YDX b (or png or jpeg).
In total there may be 1000, 2000, 3000 photos or more in the folder and for each product there may be 1 or 2 or 3 or 15, etc. photos
Example
-
Product PHOTOS ac2345
- ac2345.png
- ac2345a.jpg
- ac2345b.png
-
Product PHOTOS 106
- 106.jpeg
- 106a.jpg
- 106b.jpg
- 106c.jpg
- 106d.jpg
-
Product PHOTOS 023198AA
- 023198AA.png
- 023198AAa.png
- 023198AAb.jpg
-
GDV7YDX product PHOTOS
- GDV7YDX.png
- GDV7YDXa.png
- GDV7YDXb.jpg
- GDV7YDXc.jpeg
- GDV7YDXd.jpg
- GDV7YDXe.png
Code
The code I submit searches all the files in a folder and writes the names of the files on one sheet but writes each name in a separate cell and all in column A.
Example:
Cell A1 = ac2345.png
Cell A2 = ac2345a.jpg
Cell A3 = ac2345b.png
Cell A4 = 106.jpeg
Cell A5 = 106a.jpg
Cell A6 = 106b.jpg
Cell A7 = 106c.jpg
Cell A8 = 106d.jpg
Cell A9 = 023198AA.png
Cell A10 = 023198AAa.png
Cell A11 = 023198AAb.jpg
Cell A12 = GDV7YDX.png
Cell A13 = GDV7YDXa.png
Cell A14 = GDV7YDXb.jpg
Cell A15 = GDV7YDXc.jpeg
Cell A16 = GDV7YDXd.jpg
Cell A17 = GDV7YDXe.png
Problem
What I need is that in the same cell the names for each product are separated by commas.
Example:
Cell A1 = ac2345.png, ac2345a.jpg, ac2345b.png
Cell A2 = 106.jpeg, 106a.jpg, 106b.jpg, 106c.jpg, 106d.jpg
Cell A3 = 023198AA.png, 023198AAa.png, 023198AAb.jpg
Cell A4 = GDV7YDX.png, GDV7YDXa.png, GDV7YDXb.jpg, GDV7YDXc.jpeg, GDV7YDXd.jpg, GDV7YDXe.png
Here is the code I have:
Sub GetJPGandPNGandJPEG()
Dim X As Long, LastDot As Long, Path As String, FileName As String, F(0 To 9) As String
Path = "C:\teste\"
FileName = Dir(Path & "*.*p*g")
Do While Len(FileName)
LastDot = InStrRev(FileName, ".")
If LCase(Mid(FileName, LastDot)) = ".jpg" Or LCase(Mid(FileName, LastDot)) = ".png" Or LCase(Mid(FileName, LastDot)) = ".jpeg" Then
If Left(FileName, 1) Like "#" Then
F(Left(FileName, 1)) = F(Left(FileName, 1)) & ", " & FileName
End If
End If
FileName = Dir
Loop
For X = 0 To 9
Cells(X + 1, "A").Value = Mid(F(X), 3)
Next
Range("A1:A10").SpecialCells(xlBlanks).Delete
End Sub
Can anyone help me? Thank you in advance.