Descending order in Userform ComboBox?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
Office Version
  1. 365
Platform
  1. Windows
Happy Friday!

Here's my code that lists folders in a UserForm ComboBox. This works fine but the results are oldest first (ascending). Is it possible to make the order descending?

VBA Code:
    Dim fso As Object
    Dim MySubFolder As Object
    Dim MyPath  As String
    
    MyPath = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
       
    For Each MySubFolder In fso.GetFolder(MyPath).SubFolders
        Archive_Open_Year.AddItem MySubFolder.Name
    Next MySubFolder
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The order is by the date of creation. If you want the modification date change this line:
Call add_date(MySubFolder.DateCreated, MySubFolder.Name)

For this:
Call add_date(MySubFolder.DateLastModified, MySubFolder.Name)

Replace all your code for this:

VBA Code:
Option Explicit

Dim filedates As New Collection
Dim filenames As New Collection

Private Sub UserForm_Activate()
  Dim fso As Object
  Dim MySubFolder As Object
  Dim MyPath  As String
  Dim i As Long
 
  MyPath = "\\GLC-SERVER\Pulte\Z_Pulte Master Archive\"

  Set fso = CreateObject("Scripting.FileSystemObject")
    
  For Each MySubFolder In fso.GetFolder(MyPath).SubFolders
    Call add_date(MySubFolder.DateCreated, MySubFolder.Name)
  Next MySubFolder
 
  For i = 1 To filenames.Count
    Archive_Open_Year.AddItem filenames(i)
  Next
End Sub

Sub add_date(date1 As Date, name1 As String)
  Dim i As Long
  For i = 1 To filedates.Count
    If filedates(i) <= date1 Then
      filedates.Add date1, before:=i
      filenames.Add name1, before:=i
      Exit Sub
    End If
  Next
  filedates.Add date1
  filenames.Add name1
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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