03-04-2021_nome file
10-12-2019__nome file
...
09-12-2017__nome file
I have this file in dir.
i need to insert the list of files in a combo box.
but before order files by date...
SORT file in dir
-
- Administrator
- Posts: 78446
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SORT file in dir
Can't you name the files like 2021-02-26_nome file? Then they automatically would be sorted correctly .
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4353
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 78446
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SORT file in dir
Use code like this:
Code: Select all
Const strFolder = "C:\MyDir\"
Dim strFile As String
Dim s As String
Dim a() As String
Dim b() As String
Dim p() As String
Dim i As Long
Dim j As Long
Dim tmp As String
Dim n As Long
strFile = Dir(strFolder & "*.*")
Do While strFile <> ""
n = n + 1
ReDim Preserve a(1 To n)
ReDim Preserve b(1 To n)
s = Left(strFile, 10)
p = Split(s, "-")
s = p(2) & p(1) & p(0)
a(n) = strFile
b(n) = s
strFile = Dir
Loop
For i = 1 To n - 1
For j = i + 1 To n
If b(j) < b(i) Then
tmp = a(i)
a(i) = a(j)
a(j) = tmp
tmp = b(i)
b(i) = b(j)
b(j) = tmp
End If
Next j
Next i
ComboBox1.List = a
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4913
- Joined: 31 Aug 2016, 09:02
Re: SORT file in dir
Amazing my tutor.
Here's another similar approach
Here's another similar approach
Code: Select all
Private Sub UserForm_Initialize()
Dim v, fso As Object, oFolder As Object, arr As Object, oFile As Object, sFile As String, i As Long
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(ThisWorkbook.Path & "\TestFolder")
Set arr = CreateObject("System.Collections.ArrayList")
For Each oFile In oFolder.Files
sFile = CStr(Split(oFile.Name, ".")(0))
v = Split(Split(sFile, "_")(0), "-")
arr.Add v(2) & v(1) & v(0) & Chr(2) & sFile
Next oFile
ReDim a(0 To arr.Count - 1)
arr.Sort
For i = 0 To arr.Count - 1
a(i) = Split(arr(i), Chr(2))(1)
Next i
ComboBox1.List = a
End Sub
-
- Administrator
- Posts: 78446
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SORT file in dir
@Yasser:
I tried code like that, but the line
Set arr = CreateObject("System.Collections.ArrayList")
causes an Automation Error on my PC.
I tried code like that, but the line
Set arr = CreateObject("System.Collections.ArrayList")
causes an Automation Error on my PC.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4913
- Joined: 31 Aug 2016, 09:02
Re: SORT file in dir
I think that is because of the NET framework 3.5 as from a long time, I got that error. Try installing NET framework 3.5
-
- Administrator
- Posts: 78446
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: SORT file in dir
I'd rather not install .NET Framework if it's not absolutely necessary.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4913
- Joined: 31 Aug 2016, 09:02
Re: SORT file in dir
It is necessary for me as I am using selenium in VBA too.