|
-
Feb 2nd, 2007, 03:58 AM
#1
Thread Starter
New Member
[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..
Last edited by spectator; Feb 2nd, 2007 at 04:03 AM.
-
Feb 2nd, 2007, 05:48 AM
#2
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...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 2nd, 2007, 10:39 PM
#3
Thread Starter
New Member
[RESOLVED] sorting record in Listbox
Hi koolsid... Thanks you very much, records are now sorted thanks very much again for the help..
-
Jul 11th, 2007, 09:55 AM
#4
Frenzied Member
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
If you find my thread helpful, please remember to rate me 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|