Sorting multicolumn listbox

K

Ken Warthen

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
 
J

joel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top