Sorting multicolumn listbox
This code adds data to a userbox and then sorts the box on the 2nd column
Sub test()
'fill list box
Set Mybox = UserForm1.ListBox1
Mybox.Clear
Mybox.ColumnCount = 2
For i = 0 To 9
Mybox.AddItem i
Mybox.List(i, 1) = Chr(Asc("A") + i)
Next i
'sort list box by column 2
NumRows = Mybox.ListCount
For i = 0 To (NumRows - 2)
For j = (i + 1) To (NumRows - 1)
If StrComp(Mybox.List(i, 1), Mybox.List(j, 1), _
vbTextCompare) = 1 Then
Temp = Mybox.List(i, 0)
Mybox.List(i, 0) = Mybox.List(j, 0)
Mybox.List(j, 0) = Temp
Temp = Mybox.List(i, 1)
Mybox.List(i, 1) = Mybox.List(j, 1)
Mybox.List(j, 1) = Temp
End If
Next j
Next i
End Sub
"Ken Warthen" wrote:
I have a listbox on an Excel userform with two columns; storenumber and
groupname. I'm currently adding each item that meets a selected criteria as
I search through a named range. I would like to display the listbox contents
sorted by the storenumber. Any idea on how I might do this? I've seen some
sorting routines, but nothing that deals with multicolumn lists.
TIA,
Ken
|