4
In February I asked for help in a code to group photo names I have in a folder (product photos) belonging to the same product, such as can be seen in this other question. My problem has been solved, but I need an adjustment to the code that I will explain.
Everything works perfectly if the names don’t match but if for example I have the photos:
Product
Production_4200a.jpg
Product
Product
Production_42001a.jpg
Production_42001b.jpg
when I run the code it joins all these references in the same cell as if it were just a reference...
Product_4200 (6 photos)
Production_4200.jpg, Product_4200a.jpg, Product_42000.jpg, Product_42001.jpg, Product_42001a.jpg, Product_42001b.jpg
... and not 3 different products:
Product_4200 (2 photos)
Production_4200.jpg, Product_4200a.jpg
Product_42000 (1 photo)
Product
Product_42001 (3 photos)
Production_42001.jpg, Product_42001a.jpg, Product_42001b.jpg
Can you help me? I hope I haven’t been too confused in my explanation. The code I have is this::
------------------ 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 possible.
Hello Francisco! Very cool your question! I think a good option would be to check the last digit to see if it is letter or number. From what I understand the code of the photo is repeated (for the same product) in letters (a, b, c, d, etc.) correct? If you have a number (0, 1, 2 etc.) it would be a new product, yes? I can help with the code, but it is not difficult to test the last character. In case I can’t solve tomorrow put the code for you! Ok? Abs
– Evert
Hello Evert, thank you for your interest. If it is a number it will be a new product. If you can help me with this last character test, I’d appreciate it, but would it be using the "If Isnumeric... Then" function? My problem is to fit this all together without ruining the code that they kindly passed me. : ) Thanks again, Abs - Francisco
– xicosantos
Francisco, I was able to do the test if it is number, but it didn’t work perfectly, because when Excel (Windows) searches the files in the folder, it comes in alphabetical order, being the numbers before the letters, which does not meet the code above, which was very well written by the way. I explain: are the files 'P_4200', 'P_4200a' and 'P_42000', this would be the ideal order, but Excel brings me 'P_4200', 'P_42000' and 'P_4200a'... =( anyway... I tried to make some adjustments and extra functions, but I think it will be easier to make a new code... I will try and put here as soon as I can. Okay? Sorry for the delay! =)
– Evert
Okay Evert... thank you very much... :)
– xicosantos
Hello. I edited the question to replace the reproduction of the content with the link of the previous question. It is more readable for everyone. I also took the thanks. It is that this site is not a forum, so they are not really necessary. By the way, if you haven’t done it yet, please do the [tour]. Good luck! :)
– Luiz Vieira
Hi again. rs So I’m very glad that your problem has been solved. However, know that the best way for you to thank @Evert for his help is by accepting his response (by clicking on the "v" next to the votes) and even by voting positively for it. :)
– Luiz Vieira