Results 1 to 4 of 4

Thread: [RESOLVED] sorting record in Listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    2

    Resolved [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:
    1. Private Sub UserForm_Initialize()
    2.     Open "C:\Refer\Refer01.txt" For Input As #1
    3.     Do While Not EOF(1)
    4.         i = 0
    5.         Line Input #1, inputline$
    6.         ListBox1.AddItem inputline$, i
    7.     Loop
    8.     ListBox1.Text = ListBox1.List(1, 0)
    9.     Close 1
    10. End Sub

    How should i go about this?

    thanks in advance guys..
    Last edited by spectator; Feb 2nd, 2007 at 04:03 AM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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:
    1. Sub SortListBox(oLb As MSForms.ListBox)
    2.     Dim vaItems As Variant
    3.     Dim i As Long, j As Long
    4.     Dim vTemp As Variant
    5.    
    6.     ‘Put the items in a variant array
    7.     vaItems = oLb.List
    8.    
    9.     For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
    10.         For j = i + 1 To UBound(vaItems, 1)
    11.             If vaItems(i, 0) > vaItems(j, 0) Then
    12.                 vTemp = vaItems(i, 0)
    13.                 vaItems(i, 0) = vaItems(j, 0)
    14.                 vaItems(j, 0) = vTemp
    15.             End If
    16.         Next j
    17.     Next i
    18.    
    19.     ‘Clear the listbox
    20.     oLb.Clear
    21.    
    22.     ‘Add the sorted array back to the listbox
    23.     For i = LBound(vaItems, 1) To UBound(vaItems, 1)
    24.         oLb.AddItem vaItems(i, 0)
    25.     Next i
    26.    
    27. 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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    2

    Resolved [RESOLVED] sorting record in Listbox

    Hi koolsid... Thanks you very much, records are now sorted thanks very much again for the help..

  4. #4
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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
  •  



Click Here to Expand Forum to Full Width