All of my tests sorted my subfolders using my first method. So, I am not sure how much better this will be. Maybe you have Window's Explorer set to sort in an order different than mine.
I used a vb.net list sorting method here. Windows 8 has v4.5 vb.net framework files installed already. If this does not work then you probably have an old version of Windows with old vb.net framework files without this method.
I test code before I post so I posted some simple test routines as well. I also showed how to sort either a string or a variant array.
As before, in an Module:
Option Explicit
Sub Test_vArraySort()
Dim a() As Variant
a() = Array("b", "c", "a")
MsgBox Join(vArraySort(a), vbLf)
End Sub
Sub Test_sArraySort()
Dim a(2) As String
a(0) = "b"
a(1) = "a"
a(2) = "c"
MsgBox Join(sArraySort(a), vbLf)
End Sub
' http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Function sArraySort(anArray() As String) As Variant
Dim myAL As Object, myALSorted As Object
Dim element As Variant, a() As Variant
Set myAL = CreateObject("System.Collections.ArrayList")
With myAL
For Each element In anArray()
.Add (element)
Next element
.Sort
sArraySort = .ToArray()
End With
End Function
Function vArraySort(anArray() As Variant) As Variant
Dim myAL As Object, myALSorted As Object
Dim element As Variant, a() As Variant
Set myAL = CreateObject("System.Collections.ArrayList")
With myAL
For Each element In anArray()
.Add (element)
Next element
.Sort
vArraySort = .ToArray()
End With
End Function
Sub Test_FoldersInParent()
MsgBox FoldersInParent(ThisWorkbook.Path)
End Sub
Function FoldersInParent(sParent As String) As String
Rem Needs Reference: MicroSoft Scripting Runtime, scrrun.dll
Rem Instructions: http://support.microsoft.com/default.aspx?scid=kb;en-us;186118
Dim FSO As New FileSystemObject
Dim fd As Folder
Dim s As String
s = vbNullString
With FSO
If Not (.FolderExists(sParent)) Then Exit Function
For Each fd In .GetFolder(sParent).SubFolders
s = s & fd.Name & vbLf
Next fd
End With
s = Left(s, Len(s) - 1)
FoldersInParent = s
End Function
In a sheet's code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim s As String, sa() As String, a() As Variant, v As Variant
With Target
If .Count <> 1 Then Exit Sub
'Change A1 to suit
If .Address <> "$A$1" Then Exit Sub
On Error Resume Next
With .Validation
.Delete
'Change ThisWorkbook.Path to suit for Parent Folder.
s = FoldersInParent(ThisWorkbook.Path)
sa() = Split(s, vbLf)
s = Join(sArraySort(sa()), ",")
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=s
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End With
End Sub
Bookmarks