Results 1 to 8 of 8

Sorting an Array

Threaded View

  1. #1
    Registered User
    Join Date
    04-15-2013
    Location
    new york city
    MS-Off Ver
    Excel 2010
    Posts
    24

    Sorting an Array

    Hi all -

    I am learning VBA and need some help understanding how the following code is able to organize an array of strings in alphabetical order. With regard to the For...Next section of code, I'd like to understand how checking if strArray(X) > strArray (Y) results in an alphabetized list. I'm also confused by the assignments - what does it mean when strTemp = strArray(X) and yet in the very next line of code strArray(X) = strArray(Y), and so on. If someone could explain in words how this section alphabetizes the array I'd really appreciate it. Thanks!
    ________________________________________________________________________________________________________________________

    Option Explicit
      Option Base 1
      
      Sub Sort_an_Array()
          
          'declare the array and other variables
          Dim strArray(12) As String
          Dim strTemp As String
          Dim strMsg As String
          Dim X As Integer, Y As Integer, i As Integer
        
          'assign strings to the array
          strArray(1) = "nihilism"
          strArray(2) = "defeatism"
          strArray(3) = "hope"
          strArray(4) = "gloom"
          strArray(5) = "euphoria"
          strArray(6) = "despondency"
          strArray(7) = "optimism"
          strArray(8) = "pessimism"
          strArray(9) = "misery"
          strArray(10) = "happiness"
          strArray(11) = "bliss"
          strArray(12) = "mania"
          
          strMsg = "Current items in array:" & vbCr & vbCr
          For i = 1 To UBound(strArray)
              strMsg = strMsg & i & ":" & vbTab & strArray(i) & vbCr
          Next i
          MsgBox strMsg, vbOKOnly + vbInformation, "Array Sorting: 1"
          
          For X = LBound(strArray) To (UBound(strArray) - 1)
              For Y = (X + 1) To UBound(strArray)
                  If strArray(X) > strArray(Y) Then
                      strTemp = strArray(X)
                      strArray(X) = strArray(Y)
                      strArray(Y) = strTemp
                      strTemp = ""
                  End If
              Next Y
          Next X
          
          strMsg = "Items in sorted array:" & vbCr & vbCr
          For i = 1 To UBound(strArray)
              strMsg = strMsg & i & ":" & vbTab & strArray(i) & vbCr
          Next i
          MsgBox strMsg, vbOKOnly + vbInformation, "Array Sorting: 2"
          
      End Sub
    Last edited by vba_beginner; 04-23-2013 at 10:33 PM. Reason: code text

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