[RESOLVED] sorting record in Listbox
Hi guys and gurus, i'm new to VB programming so please bare with me :)
i'm making a MACRO shortcut in MSWord which will invoke name of people. I'm using the bundled VB program of MSWord under macros. My problem is, how would i be able to show the names in alphabetical order?.. here's my simple program for a simple minded like me :)
C:\Refer\Refer01.txt (input)
Poohkyaw, Many
Yu, Ester
Polopot, Patty
Listbox1 (output)
Polopot, Patty
Poohkyaw, Many
Yu, Ester
VB Code:
Private Sub UserForm_Initialize()
Open "C:\Refer\Refer01.txt" For Input As #1
Do While Not EOF(1)
i = 0
Line Input #1, inputline$
ListBox1.AddItem inputline$, i
Loop
ListBox1.Text = ListBox1.List(1, 0)
Close 1
End Sub
How should i go about this?
thanks in advance guys..
Re: sorting record in Listbox
Hi
Does this helps?
Here’s a sub that you can call to sort the data in a ListBox. It sorts in alphabetical order.
VB Code:
Sub SortListBox(oLb As MSForms.ListBox)
Dim vaItems As Variant
Dim i As Long, j As Long
Dim vTemp As Variant
‘Put the items in a variant array
vaItems = oLb.List
For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
For j = i + 1 To UBound(vaItems, 1)
If vaItems(i, 0) > vaItems(j, 0) Then
vTemp = vaItems(i, 0)
vaItems(i, 0) = vaItems(j, 0)
vaItems(j, 0) = vTemp
End If
Next j
Next i
‘Clear the listbox
oLb.Clear
‘Add the sorted array back to the listbox
For i = LBound(vaItems, 1) To UBound(vaItems, 1)
oLb.AddItem vaItems(i, 0)
Next i
End Sub
Hope this helps...
[RESOLVED] sorting record in Listbox
Hi koolsid... Thanks you very much, records are now sorted :thumb: thanks very much again for the help..
Re: [RESOLVED] sorting record in Listbox
Sorry to hijack this thread, but what if I have a listbox which has more than one column, and i want to sort according to the contents of the first column?
cheers