Hi,
I seem unable to find any VBA collection or even a scripting dictionary that has an in-built sort functionality.
Is this correct?
I'm currently using this bubble sort code...
Which I am having to use to populate a list box on a from with the content of a directory like so...
This seems ridiculously convoluted just to be able to have a sorted collection... so is there a better way?
Thanks,
1DMF.
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
I seem unable to find any VBA collection or even a scripting dictionary that has an in-built sort functionality.
Is this correct?
I'm currently using this bubble sort code...
Code:
Public Function sortCol(ByVal cCol As Collection) As Collection
Dim i As Long
Dim x As Long
Dim vOld As Variant
For i = 1 To cCol.Count - 1
For x = i + 1 To cCol.Count
If cCol(i) > cCol(x) Then
vOld = cCol(x)
cCol.Remove x
cCol.Add vOld, vOld, i
End If
Next x
Next i
Set sortCol = cCol
End Function
Which I am having to use to populate a list box on a from with the content of a directory like so...
Code:
Private Sub Form_Open(Cancel As Integer)
Dim sPath As String
Dim sFile As String
Dim vCode As Variant
Dim cCol1 As New Collection
Dim cCol2 As New Collection
' remove current list items
Do While Me.Pre_Approved.ListCount > 0
Me.Pre_Approved.RemoveItem (0)
Loop
' get files in directory
sPath = cDrive & "Members Database\AccessTemplates\Financial_Promotions\"
sFile = Dir(sPath & "*.*", vbDirectory)
' loop and add to collection
Do While sFile <> ""
If sFile <> "." And sFile <> ".." Then
vCode = Split(sFile, "-")
cCol1.Add left(vCode(UBound(vCode)), 7), left(vCode(UBound(vCode)), 7)
cCol2.Add (sPath & sFile), left(vCode(UBound(vCode)), 7)
End If
sFile = Dir
Loop
' sort collection
Set cCol1 = sortCol(cCol1)
' add to listbox and remove from collection
Do While cCol1.Count > 0
Me.Pre_Approved.addItem Item:=Chr(34) & cCol1.Item(1) & Chr(34) & Chr(59) & Chr(34) & cCol2.Item(cCol1(1)) & Chr(34)
cCol1.Remove 1
Loop
' clear collections
Set cCol1 = Nothing
Set cCol2 = Nothing
End Sub
Thanks,
1DMF.
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music