Sort Directory files in listbox

Mumba

New Member
Joined
May 3, 2018
Messages
14
Hi All

I have a code that loads files from a specific directory. All files at named based on a simple dateformat i.e 01012018 or 19122018.
My issues is that when the files are loaded into the listbox, they are not sortet. Sorting the folder does not solve my problem. All i really want is for the newest file to be on top and so forth.
Please help

Code for loading looks like this and works

Code:
Private Sub UserForm_Initialize()
 
fpath = ThisWorkbook.path & "\Input\"

ListBox1.Clear
    orderfile = Dir(fpath & "*.xls*", vbNormal)
     Do While Len(orderfile) > 0
        ListBox1.AddItem orderfile
        orderfile = Dir()
    Loop
    
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] UserForm_Initialize()
    
    fpath = ThisWorkbook.Path & "\Input\"
    
    [color=darkblue]With[/color] Me.ListBox1
    
        .Clear
        orderfile = Dir(fpath & "*.xls*", vbNormal)
        [color=darkblue]Do[/color] [color=darkblue]While[/color] Len(orderfile) > 0
            .AddItem orderfile
            orderfile = Dir()
        [color=darkblue]Loop[/color]
        
        [color=green]'sort[/color]
        [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color], j [color=darkblue]As[/color] Long, tmp [color=darkblue]As[/color] [color=darkblue]String[/color]
        [color=darkblue]For[/color] i = 0 [color=darkblue]To[/color] .ListCount - 2
            [color=darkblue]For[/color] j = i + 1 [color=darkblue]To[/color] .ListCount - 1
                [color=darkblue]If[/color] DateValue(Format(Left(.List(i), 8), "00/00/0000")) < DateValue(Format(Left(.List(j), 8), "00/00/0000")) [color=darkblue]Then[/color]
                    tmp = .List(i)
                    .List(i) = .List(j)
                    .List(j) = tmp
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Next[/color] j
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top