here is how you can do it
Sub SortItemInListBox() '
'assume you have a userform and a listbox
'
Dim SortNow As New Collection, my_I As Integer
Dim i As Integer, j As Integer
Dim countItem As Integer
Dim Swap1, Swap2, Item, myItem
'add item to listbox
With UserForm1.ListBox1
.AddItem "Orange"
.AddItem "Apple"
.AddItem "Pear"
.AddItem "Durain"
.AddItem "Papaya"
End With
'add listbox item to collection
countItem = UserForm1.ListBox1.ListCount
For my_I = 0 To countItem - 1
myItem = UserForm1.ListBox1.List(my_I)
SortNow.Add Item:=myItem
Next
'Resume normal error handling
On Error GoTo 0
'Sort the collection
For i = 1 To countItem - 1
For j = i + 1 To countItem
If SortNow(i) > SortNow(j) Then
Swap1 = SortNow(i)
Swap2 = SortNow(j)
SortNow.Add Swap1, Before:=j
SortNow.Add Swap2, Before:=i
SortNow.Remove i + 1
SortNow.Remove j + 1
End If
Next j
Next i
'Clear list box
UserForm1.ListBox1.Clear
'add sorted item to listbox
For Each Item In SortNow
UserForm1.ListBox1.AddItem Item
Next Item
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.