ORDER items in listbox

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

ORDER items in listbox

Post by sal21 »

To fill a list box i use this code:

Code: Select all

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.

User avatar
HansV
Administrator
Posts: 78608
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ORDER items in listbox

Post by HansV »

Do you mean that 12_2011 should come before 01_2012?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: ORDER items in listbox

Post by sal21 »

HansV wrote:Do you mean that 12_2011 should come before 01_2012?
Yes, Perfect!!!!!

but from the newst date to the old date... in my case:

03_2012
02_2012
...
12_2011

User avatar
HansV
Administrator
Posts: 78608
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ORDER items in listbox

Post by HansV »

So you mean that 12_2011 should come AFTER 01_2012?
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78608
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ORDER items in listbox

Post by HansV »

Try this:

Code: Select all

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
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: ORDER items in listbox

Post by sal21 »

HansV wrote:Try this:

Code: Select all

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
No Word!!!!!! :thankyou:

Note:
Sorry for your time and patience. :grin: :clapping: