0

I have part of code which goes over the files of a folder and assigns a number to them. These files are zip files generated with a "run number-date-time.7z" name format. Their numbers and date/time stamps are in order. It is important for my application that the files are read by the Dir()-loop in the correct order also but that does not happen (see attached image).

Can someone help me understand why this is and how to correct it?

Any help is much appreciated!

Sub Test()

    Dim sPath As String, archiveFiles() As String
    Dim wbTarget As Workbook
    Dim rSrc As Range
    Dim rDst As Range
    
    targetWorkbook = ThisWorkbook.Name
    sPath = ThisWorkbook.Path
    
    folderName = "D:\New folder\Archives"
    
   
    'Look in folder for archive files
    If Right(folderName, 1) <> "\" Then
        folderName = folderName & "\"
    End If
    
    j = 0
    archiveFile = Dir(folderName & "*.7z")
    Do Until archiveFile = vbNullString
        ReDim Preserve archiveFiles(j + 1)
        archiveFiles(j) = folderName & "\" & archiveFile
        archiveFile = Dir()
        j = j + 1
    Loop


End Sub

Locals result

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • 3
    There is no guaranteed order, the only way around it that I'm aware of is store the return in an array, sort the array, and then do what you need to with the files. – Warcupine May 19 '22 at 17:30
  • I've not seen the order documented. Why not just sort the resultant array by your desired order? – Ron Rosenfeld May 19 '22 at 17:32
  • FYI the `dir` command has a bunch of switches including for sorting - you can run that from VBA eg see https://stackoverflow.com/questions/37822050/cant-run-dir-from-wscript-shell-in-vba – Tim Williams May 19 '22 at 18:19

0 Answers0