+ Reply to Thread
Results 1 to 3 of 3

Thread: How to sort a listbox alphabetically?...

  1. #1
    Registered User
    Join Date
    01-02-2012
    Location
    Ebbw Vale
    MS-Off Ver
    Excel 2007
    Posts
    17

    How to sort a listbox alphabetically?...

    hi,

    I am having trouble with a small piece of my project...
    i have a userform which enables a user to enter a name and it goes into the listbox...
    i need this listbox to sort the names alphabetically...
    i have tries all sorts and just can't seem to get it..

    hope someone can help... been trying for ages and i'm not getting anywhere

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: How to sort a listbox alphabetically?...

    Hi 10121730,
    What have you tried?
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  3. #3
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: How to sort a listbox alphabetically?...

    As Pike said it is always good to see what you have done rather than writing the whole project for you. The aim of the forum is to help you with your code so you should always have a try first. No one is going to laugh..plenty of people have laughed at my code so don't sweat it. Anyway I can give you an example using a Bubble Sort (I cannot take credit for the method however it is a sort method I use a lot) - try the attached workbook - I have added some names to a listbox and then do a bubble sort on the items and then replace the values into the listbox. You will have to adapt the code for your own needs..
    The bubble sort method is as follows however you can see all the code inside the workbook
     Dim i As Long, k As Long
      Dim varT As Variant
     For i = LBound(sortItemsArr, 1) To UBound(sortItemsArr, 1) - 1
            For k = i + 1 To UBound(sortItemsArr, 1)
                If sortItemsArr(i, 0) > sortItemsArr(k, 0) Then
                    varT = sortItemsArr(i, 0)
                    sortItemsArr(i, 0) = sortItemsArr(k, 0)
                    sortItemsArr(k, 0) = varT
                End If
            Next k
        Next i
    Where sortItemsArr is the array of names from the listbox.

    Hope this helps.
    Anthony
    “Confidence never comes from having all the answers; it comes from being open to all the questions.”
    PS: Remember to mark your questions as Solved once you are satisfied and rate the answer(s) questions.”
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0