Private Sub ListFolder(sFolderPath As String)
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim subfolder As Folder
Dim I As Integer
Me.CMESI.Clear
Set FSfolder = FS.GetFolder(sFolderPath)
For Each subfolder In FSfolder.SubFolders
DoEvents
I = I + 1
With Me.CMESI
.AddItem subfolder.Name
End With
Next subfolder
Set FSfolder = Nothing
End Sub
the dir sFolderPath contain the subdir in image attached, how to order the subdir by name date in listbox?
You do not have the required permissions to view the files attached to this post.
Private Sub ListFolder(sFolderPath As String)
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim subfolder As Folder
Dim N As Long
Dim i As Long
Dim strName As String
Me.CMESI.Clear
Set FSfolder = FS.GetFolder(sFolderPath)
N = FSfolder.SubFolders.Count
ReDim arrMonth(1 To N) As Long
For Each subfolder In FSfolder.SubFolders
i = i + 1
strName = subfolder.Name
arrMonth(i) = 100 * Right(strName, 4) + Left(strName, 2)
Next subfolder
Set FSfolder = Nothing
BubbleSort arrMonth
For i = 1 To N
strName = Format(arrMonth(i) Mod 100, "00") & "_" & arrMonth(i) \ 100
Me.CMESI.AddItem strName
Next i
End Sub
Sub BubbleSort(arr)
Dim i As Long
Dim j As Long
Dim strTemp As String
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) < arr(j) Then
strTemp = arr(i)
arr(i) = arr(j)
arr(j) = strTemp
End If
Next j
Next i
End Sub
Last edited by HansV on 22 May 2012, 12:18, edited 1 time in total.
Reason:to reverse sort order
Private Sub ListFolder(sFolderPath As String)
Dim FS As New FileSystemObject
Dim FSfolder As Folder
Dim subfolder As Folder
Dim N As Long
Dim i As Long
Dim strName As String
Me.CMESI.Clear
Set FSfolder = FS.GetFolder(sFolderPath)
N = FSfolder.SubFolders.Count
ReDim arrMonth(1 To N) As Long
For Each subfolder In FSfolder.SubFolders
i = i + 1
strName = subfolder.Name
arrMonth(i) = 100 * Right(strName, 4) + Left(strName, 2)
Next subfolder
Set FSfolder = Nothing
BubbleSort arrMonth
For i = 1 To N
strName = Format(arrMonth(i) Mod 100, "00") & "_" & arrMonth(i) \ 100
Me.CMESI.AddItem strName
Next i
End Sub
Sub BubbleSort(arr)
Dim i As Long
Dim j As Long
Dim strTemp As String
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) < arr(j) Then
strTemp = arr(i)
arr(i) = arr(j)
arr(j) = strTemp
End If
Next j
Next i
End Sub