In February I asked for a code to group photo names that I have in a folder (product photos) belonging to the same product, such as you can see in this other question . My problem has been solved, but I need an adjustment to the code I'm going to explain.
Everything works perfectly if the names do not match but if for example I have the photos:
Product_4200.jpg
Product_4200a.jpg
Product_42000.jpg
Product_42001.jpg
Product_42001a.jpg
Product_42001b.jpg
When I run the code it joins these references all in the same cell as if it were just a reference ...
Product_4200 (6 photos)
Product_4200.jpg, Product_4200a.jpg, Product_42000.jpg, Product_42001.jpg, Product_42001a.jpg, Product_42001b.jpg
... and not from 3 different products:
Product_4200 (2 photos)
Product_4200.jpg, Product_4200a.jpg
Product_42000 (1 photo)
Product_42000.jpg
Product_42001 (3 photos)
Product_42001.jpg, Product_42001a.jpg, Product_42001b.jpg
Can you help me? I hope I was not too confused in my explanation. The code I have is the following:
------------------ Codigo (Paulo Balbino)-----------------------------
Sub GetJPGandPNGandJPEG()
Dim Path As String
Dim FileName As String
Dim LastDot As Long
Dim FileNameAux As String
Dim FileNameConc As String
Dim LastRow As Long
Path = "C:\Temp\Imagens\"
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 (FileNameAux = vbNullString) Then
FileNameAux = Replace(FileName, LCase(Mid(FileName, LastDot)), "")
End If
If (InStr(1, FileName, FileNameAux, vbTextCompare)) Then
If (FileNameConc = vbNullString) Then
FileNameConc = FileName
Else
FileNameConc = FileNameConc & ", " & FileName
End If
Else
If (FileNameConc = vbNullString) Then
FileNameConc = FileName
End If
LastRow = Plan1.Cells(Plan1.Rows.Count, 1).End(xlUp).Row + 1
Plan1.Cells(LastRow, 1) = FileNameConc
FileNameAux = Replace(FileName, LCase(Mid(FileName, LastDot)), "")
FileNameConc = FileName
End If
End If
FileName = Dir
Loop
LastRow = Plan1.Cells(Plan1.Rows.Count, 1).End(xlUp).Row + 1
Plan1.Cells(LastRow, 1) = FileNameConc
End Sub
---------------------------------------------------------------------
Thanks in advance for all the help you can get.