How to sort a ListBox on a UserForm in Word VBA?

I have a list of files which are insterted as Sub-Documents in a Master Document.  The file names are read into a ListBox on a UserForm.  However, they appear in the ListBox in Created Date order.  There doesn't seem to be a "Sort By" property on the ListBox, and there doesn't seem to be a Sort Method for the ListBox.
At the moment, I have to ask the end user to assemble the final order by selecting one at a time and building up the final order in a second ListBox on the same UserForm  -  OK if there are only 10 or a dozen sub-documents, but I know there's one coming up with over 100....!
Anyone have any ideas, please?
(Reasonable experience of VBA in Access and Excel, but not quite so good in Word).
All help gratefully received...!

Ed
jocktherockAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
GrahamSkanConnect With a Mentor RetiredCommented:
You need to sort the list yourself

Option Explicit

Private Sub CommandButton1_Click()
    Dim strArray() As String
    Dim i As Integer
    ReDim strArray(ListBox1.ListCount - 1)
    For i = 0 To ListBox1.ListCount - 1
        strArray(i) = ListBox1.List(i)
    Next i
    DoSort strArray()
    ListBox1.List = strArray
End Sub

Function DoSort(ByRef sortarray() As String)
Dim i As Integer
Dim j As Integer
Dim strTemp As String
    For i = 0 To UBound(sortarray())
        For j = 0 To UBound(sortarray())
            If sortarray(i) < sortarray(j) Then
                strTemp = sortarray(i)
                sortarray(i) = sortarray(j)
                sortarray(j) = strTemp
            End If
        Next j
    Next i
End Function
0
 
jocktherockAuthor Commented:
Excellent!  Worked first time!  I had tried passing the List to an Array, but couldn't get any further than that....!
And the solution came back very rapidly.
Many, many thanks!


Ed
0
All Courses

From novice to tech pro — start learning today.