View Single Post
 
Old 02-03-2023, 02:34 AM
Priyantha Gamini Priyantha Gamini is offline Windows 10 Office 2016
Novice
 
Join Date: Dec 2022
Posts: 9
Priyantha Gamini is on a distinguished road
Default Sorting File names

Dear All,

I get file names to an excel sheet from a folder, using the below macro. I tried to sort them in ascending order. but I have not sorted them in the correct order. please can anyone solve this issue?

Correct oder : 1-1.pdf, 1-2.pdf, 1-3.pdf ............... 1-10.pdf, 1-11.pdf......1-20.pdf..

After getting to the excel sheet: 1-1.pdf, 1-10.pdf, 1-11.pdf, 1-20.pdf........

My VBA Code :

Sub Get_Old_File_Name()
Dim myPath As String
Dim myFile As String
Dim r As Long

myPath = "C:\Users\User\Desktop\Maling Payadvice\NVQ\PaySlips"
myFile = Dir(myPath & "*.*")

If myFile = "" Then
MsgBox " No Files in the Folder"
Else

r = 4

Do While myFile <> ""
Cells(r, 5).Value = myFile
r = r + 1
myFile = Dir

Loop
MsgBox "Completed"
End If
End Sub
Attached Images
File Type: jpg Correct Oder.jpg (137.8 KB, 10 views)
File Type: jpg In the Excel Sheet Oder.jpg (235.4 KB, 10 views)
Reply With Quote