I want to list all files in a folder, but order isn't correct. I have file1, file2 to file15. Unfortunately file10 will be listed before file2. I have no influance on the names of the files, but I have to sort them like file1, file2 ... file9, file10 and so on.
Sub orderFiles()
Dim strFolder As String
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim strWert As Variant
Dim n As Long
Dim arrNames() As String
Dim x As Long, y As Long
Dim TempTxt1 As String
Dim TempTxt2 As String
Set fso = New Scripting.FileSystemObject
' Modify as needed
strFolder = p_PR1ImportPath & "\PR1\importiert"
Set fld = fso.GetFolder(strFolder)
' Set up arrays
n = fld.Files.Count
ReDim arrNames(1 To n)
' Fill arrays
For Each fil In fld.Files
i = i + 1
arrNames(i) = fil.Name
Next fil
'Alphabetize Sheet Names in Array List
For x = LBound(arrNames) To UBound(arrNames)
For y = x To UBound(arrNames)
If UCase(arrNames(y)) < UCase(arrNames(x)) Then
TempTxt1 = arrNames(x)
TempTxt2 = arrNames(y)
arrNames(x) = TempTxt2
arrNames(y) = TempTxt1
End If
Next y
Next x
' Do something with the arrays, e.g.
For i = 1 To n
Debug.Print arrNames(i)
Next i
End Sub
The result isn't changed. How can I sort the list of files?