Consulting

Results 1 to 4 of 4

Thread: Stuck on sorting array

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Posts
    10
    Location

    Stuck on sorting array

    Hi there! I am self-learning excel vba recently following online materials. Now I am learning sorting array (with nested loop). There is something I couldn't figure it out.
    Could any one tell me what the difference between this two codes? Both of them got the same result. I've tried to study it for a day but still can't understand the concept. Thank you!

    Answer from the materials:

    For j = i + 1 To UBound(myArray)            '
            If UCase(myArray(i)) > UCase(myArray(j)) Then
                Temp = myArray(j)
                myArray(j) = myArray(i)
                myArray(i) = Temp
            End If
    Tested by myself:
    For j = i + 1 To UBound(myArray)
            If UCase(myArray(i)) > UCase(myArray(j)) Then
                Temp = myArray(i)
                myArray(i) = myArray(j)
                myArray(j) = Temp
            End If
    The full code is below:

    Sub vba_sort_array()
    Dim myArray() As Variant
    ReDim myArray(5)
    Dim i As Integer
    Dim j As Integer
    Dim Temp As String
    myArray(1) = "E"
    myArray(2) = "D"
    myArray(3) = "C"
    myArray(4) = "B"
    myArray(5) = "A"
    MsgBox LBound(myArray)
    MsgBox UBound(myArray)
    'sorting array from A to Z
    For i = LBound(myArray) To UBound(myArray)
    For j = i + 1 To UBound(myArray)            '
            If UCase(myArray(i)) > UCase(myArray(j)) Then
                Temp = myArray(j)
                myArray(j) = myArray(i)
                myArray(i) = Temp
            End If
    '     For j = i + 1 To UBound(myArray)
    '        If UCase(myArray(i)) > UCase(myArray(j)) Then
    '            Temp = myArray(i)
    '            myArray(i) = myArray(j)
    '            myArray(j) = Temp
    '        End If
    'Next j
    Next i
    Debug.Print myArray(1)
    Debug.Print myArray(2)
    Debug.Print myArray(3)
    Debug.Print myArray(4)
    Debug.Print myArray(5)
    End Sub
    Last edited by Aussiebear; 09-15-2023 at 06:07 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    There is no functional difference; both of them swap the items around. The only change is which one is put into a temporary variable while you assign the other to its place.
    Be as you wish to seem

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Sorry, couldn't resist some comments and tweaks

    Sub vba_sort_array()
        Dim myArray() As Variant
        
        ReDim myArray(1 To 5)   '   other wise starts at 0 (unless you have Option Base 1 set (homework assignment ))
        Dim i As Integer
        Dim j As Integer
        Dim Temp As String
        
        myArray(1) = "E"
        myArray(2) = "D"
        myArray(3) = "C"
        myArray(4) = "B"
        myArray(5) = "A"
        
        MsgBox LBound(myArray) & " - " & UBound(myArray)
        
        'sorting array from A to Z
        For i = LBound(myArray) To UBound(myArray) - 1      '   added the -1
            For j = i + 1 To UBound(myArray)            '
                If UCase(myArray(i)) > UCase(myArray(j)) Then
                    Temp = myArray(j)
                    myArray(j) = myArray(i)
                    myArray(i) = Temp
                End If
            Next j
        Next i
        
        Debug.Print myArray(1)
        Debug.Print myArray(2)
        Debug.Print myArray(3)
        Debug.Print myArray(4)
        Debug.Print myArray(5)
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Nov 2011
    Posts
    10
    Location
    Thank you for your explanation!

Posting Permissions

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