+ Reply to Thread
Results 1 to 12 of 12

Sort Array Alphabetically

Hybrid View

gsurge Sort Array Alphabetically 10-04-2012, 10:21 AM
patel45 Re: Sort Array Alphabetically 10-04-2012, 10:37 AM
gsurge Re: Sort Array Alphabetically 10-04-2012, 10:45 AM
Leith Ross Re: Sort Array Alphabetically 10-04-2012, 11:17 AM
gsurge Re: Sort Array Alphabetically 10-04-2012, 11:36 AM
Kyle123 Re: Sort Array Alphabetically 10-04-2012, 11:11 AM
mike7952 Re: Sort Array Alphabetically 10-04-2012, 11:19 AM
gsurge Re: Sort Array Alphabetically 10-04-2012, 11:40 AM
nilem Re: Sort Array Alphabetically 10-04-2012, 11:58 AM
gsurge Re: Sort Array Alphabetically 10-04-2012, 12:03 PM
nilem Re: Sort Array Alphabetically 10-04-2012, 11:29 AM
Leith Ross Re: Sort Array Alphabetically 10-04-2012, 11:42 AM
  1. #1
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Exclamation Sort Array Alphabetically

    Hello,

    I am successfully able to sort through a range of cells and allocate names to various arrays based on specified criteria. Those arrays are then used to populate listboxes in a userform.

    I am stuck on how to sort the array alphabetically before populating the listbox with the names.

    Any help would be great...please see bottom of code below for populating listbox section

    '!CREATE NEW ARRAY TO RETRIEVE MANAGER NAMES FOR POSITIONS IN THE ORIGINAL ARRAY THAT MEET CRITERIA (HAVE VALUES)!
    
    
    RowCounter = 6
    ListCounter = 0
    Do While Not IsEmpty(Sheets("matrix").Range("A" & RowCounter).Value)
    
        If CorrelArray4(ListCounter) <> 0 Then
        CorrelArrayADDNAME(ListCounter) = Sheets("matrix").Range("E" & RowCounter).Value
        Else: End If
        
    ListCounter = ListCounter + 1
    RowCounter = RowCounter + 1
    Loop
    
    
    '!REMOVE BLANKS FROM ARRAYS (MANAGERS THAT DO NOT MEET CRITERIA)
    
    
    ReDim CorrelArrayADD(LBound(CorrelArrayADDNAME) To UBound(CorrelArrayADDNAME))
    For h = LBound(CorrelArrayADDNAME) To UBound(CorrelArrayADDNAME)
        If CorrelArrayADDNAME(h) <> "" Then
            CorrelArrayADD(K) = CorrelArrayADDNAME(h)
        K = K + 1
        End If
    Next
    ReDim Preserve CorrelArrayADD(LBound(CorrelArrayADDNAME) To K)
    
    
    'Populate list boxes
    
    With OutputRep.listMANUAL
    .Clear
    .List = CorrelArrayADD
    End With
    
        OutputRep.Show
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Sort Array Alphabetically

    why not sort on sheet, then put in array ?
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Sort Array Alphabetically

    I figured that was also an option, but I thought it may be easier to do it directly in vb...can you please recommend the code for that process?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Sort Array Alphabetically

    Hello gsurge,

    This macro will sort a list in either ascending (A-Z) or descending (Z-A) order. The default is ascending. The list can be either 1-D or 2-D single column.
    Function SortArray(ByVal Data As Variant, Optional Descending As Boolean) As Variant
    
        ' Written: July 05, 2011
        ' Author:  Leith Ross
        ' Summary: Sorts a 1-D Array or 2-D Array in either ascending (default)
        '          or descending order using the Bubble sort algorithm. The function
        '          returns the original 2-D array with the first column sorted or
        '          the 1-D array as a sorted 2-D (UB x 1) array. if Data is not an
        '          array then the reutrn value is set to a boolean False.
      
        Dim arr As Variant
        Dim LB As Long
        Dim I As Long
        Dim J As Long
        Dim UB As Long
        Dim Temp As Variant
      
            Select Case TypeName(Data)
                Case Is = "Range", "Variant()"
                    arr = Data
                    On Error Resume Next
                        UB = UBound(Data, 2)
                    On Error GoTo 0
                    If UB > 0 Then
                      ' 2-D Array with multiple columns
                        arr = Data
                    Else
                      ' 1-D Array - Convert to a 2-D (UB x 1)
                        arr = WorksheetFunction.Transpose(Data)
                    End If
                Case Else
                    SortArray = False
            End Select
        
            LB = LBound(arr)
            UB = UBound(arr)
        
                For I = LB To UB
                    For J = LB To UB - 1
                        If Descending Xor (arr(I, LB) < arr(J, LB)) Then
                            Temp = arr(J, LB)
                            arr(J, LB) = arr(I, LB)
                            arr(I, LB) = Temp
                        End If
                    Next J
                Next I
            
            SortArray = arr
         
    End Function
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Sort Array Alphabetically

    Thanks Leith. I placed your code in a module and called it using:

    Call SortArray(CorrelArrayADD)
    But this didn't sort the array...did I miss a step?

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Sort Array Alphabetically


  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Sort Array Alphabetically

    try this

    Dim CorrelArrayADD
        With CreateObject("system.collections.arraylist")
            With Sheets("matrix")
            Do While Not IsEmpty(.Range("A" & RowCounter).Value)
                If Len(Trim(.Range("E" & RowCounter).Value)) > 0 Then
                    .Add .Range("E" & RowCounter).Value
                End If
                RowCounter = RowCounter + 1
            Loop
            .Sort
           CorrelArrayADD = .toarray
        End With
        With OutputRep.listMANUAL
            .Clear
            .List = CorrelArrayADD
        End With
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  8. #8
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Sort Array Alphabetically

    Nilem,

    That worked perfectly! Thank you so much!

    One quick thing...I still have a space at the beginning of my array...apparently I didn't remove it. Any idea on how to get rid of this?

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Sort Array Alphabetically

    Quote Originally Posted by gsurge View Post
    ...I still have a space at the beginning of my array...
    change this line
    ReDim Preserve CorrelArrayADD(LBound(CorrelArrayADDNAME) To k - 1)

  10. #10
    Registered User
    Join Date
    05-23-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    79

    Re: Sort Array Alphabetically

    thank you again. everything works perfectly.

  11. #11
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Sort Array Alphabetically

    as an option
    ....
    With OutputRep.listMANUAL
        .Clear
        .List = ShellSort(CorrelArrayADD)
    End With
    ....
    Function ShellSort(x)
    Dim Limit As Long, Switch As Long, i As Long, j As Long, tmp
    j = (UBound(x) - LBound(x) + 1) \ 2
    Do While j > 0
        Limit = UBound(x) - j
        Do
            Switch = LBound(x) - 1
            For i = LBound(x) To Limit
                If x(i) > x(i + j) Then
                    tmp = x(i)
                    x(i) = x(i + j)
                    x(i + j) = tmp
                    Switch = i
                End If
            Next
            Limit = Switch - j
        Loop While Switch >= LBound(x)
        j = j \ 2
    Loop
    ShellSort = x
    End Function

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Sort Array Alphabetically

    Hello gsurge,

    The macro is a Function that returns the original array as a sorted array.
    Dim Sorted Array as Variant
        SortedArray = SortArray(CorrelArrayADD)

+ 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.6.0 RC 1