Consulting

Results 1 to 4 of 4

Thread: Sort Alphabetically List Box

  1. #1
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Question Sort Alphabetically List Box

    Could anyone show me how to sort a list box alphabetically?

    Thanks.

  2. #2
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    How are you storing the data for the row source? (This was my 100th post! Yay!)

  3. #3
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    I read the data from the column in excel worksheets "Sheet1" column "C".

    How do I also to read the string value from the listbox with multiple selection?
    For example, MsgBox() for every value that is selected.

    Thanks.

  4. #4
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Thumbs up Solution

    This is the solution that I used. Hope it can help everyone.

    Function SortListBoxArray(ByRef vTemp As Variant)
    Dim vArr(), vSArr() As String, vItm As Variant, i As Long
    vArr = vTemp.List
    i = 0
    For Each vItm In vArr
    ReDim Preserve vSArr(i)
    vSArr(i) = vItm
    i = i + 1
    Next vItm
    SortStringArray vSArr
    vTemp.List = vSArr
    End Function
     
     
    Sub SortStringArray(x)
    ' Function: sorts virtually any data type from smallest to largest
    ' Limitations: assumes entire array from LBound(X) to UBound(X) is to be sorted
    ' Passed Values: X [in, any] array of values
    Dim i As Long
    Dim J As Long
    Dim temp
    Dim PerDone As Double
    For i = LBound(x) To UBound(x) - 1
    For J = i + 1 To UBound(x)
    If x(i) > x(J) Then
    temp = x(i)
    x(i) = x(J)
    x(J) = temp
    End If
    Next J
    Next i
    End Sub
    Thanks.

Posting Permissions

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