Descending order in Userform ComboBox?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
482
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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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