+ Reply to Thread
Results 1 to 3 of 3

Sorting an Array by Highest to Lowest [Excel 14, VBA Only]

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Sorting an Array by Highest to Lowest [Excel 14, VBA Only]

    I cannot figure out how to sort the remaining values. I repeated the sort but it give the highest value every time.

    As a test I was using:
    CPT_1 = 23410
    CPT_2 = 23397
    CPT_3 = 27060

    Charges_1 = 7000.
    Charges_2 = 15000.
    Charges_3 = 4000.

    FSValue_1 = 6939.45
    FSValue_2 = 11244.51
    FSValue_3 = 3524.78


    I am using the following code.

    Sub valueSortTest()
    
        '***SETTING VARIABLES***
        Dim readForm As Worksheet, cpt(1 To 3) As String, charges(1 To 3) As Double, fsValue(1 To 3) As Double
        Dim MaxSort(1 To 3, 1 To 3), x As Integer, sortValue(1 To 3, 1 To 3)
                
        '***IDENTIFYING WORKSHEET TO BE USED***
        Set readForm = ActiveWorkbook.Worksheets("Sheet1")
        
        '***READING DATA INTO AN ARRAY***
        For x = 1 To 3
            cpt(x) = readForm.Range("A" & x).Value
            charges(x) = readForm.Range("B" & x).Value
            fsValue(x) = readForm.Range("C" & x).Value
            sortValue(1, x) = cpt(x)
            sortValue(2, x) = charges(x)
            sortValue(3, x) = fsValue(x)
        Next x
        
         '***SORTING ARRRAY [FIRST TIME]***
        For x = 1 To 3
            If sortValue(3, x) > MaxSort(3, 1) Then
                MaxSort(1, 1) = sortValue(1, x)
                MaxSort(2, 1) = sortValue(2, x)
                MaxSort(3, 1) = sortValue(3, x)
            Else
            End If
        Next x
    
         
            '***PLACE SORTED VALUES INTO SPREADSHEET/FORM***
        For x = 1 To 3
            readForm.Range("E" & x).Value = MaxSort(1, x)
            readForm.Range("F" & x).Value = MaxSort(2, x)
            readForm.Range("G" & x).Value = MaxSort(3, x)
        Next x
    
    End Sub
    Attached Files Attached Files
    Last edited by lloydgodin; 03-21-2015 at 05:07 PM. Reason: more detail, thread bump

  2. #2
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting an Array by Highest to Lowest [Excel 14, VBA Only]

    I was able to get it working partially. It works with 3 values (I eventually need it to work with 10). And it only works if they are in a certain order.

        For x = 1 To 3
            If sortValue(3, x) > MaxSort(3, 1) Then
                MaxSort(1, x) = MaxSort(1, 1)
                MaxSort(2, x) = MaxSort(2, 1)
                MaxSort(3, x) = MaxSort(3, 1)
                MaxSort(1, 1) = sortValue(1, x)
                MaxSort(2, 1) = sortValue(2, x)
                MaxSort(3, 1) = sortValue(3, x)
    '            sorted(x) = True
            Else
                MaxSort(1, x) = sortValue(1, x)
                MaxSort(2, x) = sortValue(2, x)
                MaxSort(3, x) = sortValue(3, x)
            End If
        Next x

  3. #3
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting an Array by Highest to Lowest [Excel 14, VBA Only]

    Here is copy of working code:

    Sub valueSortTest()
    
        '***SETTING VARIABLES***
        Dim readForm As Worksheet, cpt(1 To 10) As String, charges(1 To 10) As Double, fsValue(1 To 10) As Double
        Dim MaxSort(1 To 3), x As Integer, sortValue(1 To 3, 1 To 10), iFirst As Integer, iLast As Integer
        Dim j As Integer
        
        
        '***IDENTIFYING WORKSHEET TO BE USED***
        Set readForm = ActiveWorkbook.Worksheets("Sheet1")
        iFirst = LBound(charges)
        iLast = UBound(charges)
        
        '***READING DATA INTO AN ARRAY***
        For x = iFirst To iLast
            cpt(x) = readForm.Range("A" & x).Value
            charges(x) = readForm.Range("B" & x).Value
            fsValue(x) = readForm.Range("C" & x).Value
            sortValue(1, x) = cpt(x)
            sortValue(2, x) = charges(x)
            sortValue(3, x) = fsValue(x)
        Next x
        
         '***SORTING ARRRAY [FIRST TIME]***
        
        For x = iFirst To iLast - 1
            For j = x + 1 To iLast
                If sortValue(3, x) < sortValue(3, j) Then
                    MaxSort(1) = sortValue(1, j)
                    MaxSort(2) = sortValue(2, j)
                    MaxSort(3) = sortValue(3, j)
                    sortValue(1, j) = sortValue(1, x)
                    sortValue(2, j) = sortValue(2, x)
                    sortValue(3, j) = sortValue(3, x)
                    sortValue(1, x) = MaxSort(1)
                    sortValue(2, x) = MaxSort(2)
                    sortValue(3, x) = MaxSort(3)
                End If
            Next j
        Next x
                 
        '***PLACE SORTED VALUES INTO SPREADSHEET/FORM***
        For x = iFirst To iLast
            readForm.Range("E" & x).Value = sortValue(1, x)
            readForm.Range("F" & x).Value = sortValue(2, x)
            readForm.Range("G" & x).Value = sortValue(3, x)
        Next x
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. sorting out values from highest to lowest using VBA
    By royalB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2014, 08:22 AM
  2. Sorting by the sum of a group from highest to lowest
    By Boosh1069 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2013, 05:58 PM
  3. Sorting a column of figures into highest to lowest
    By josand in forum Excel General
    Replies: 4
    Last Post: 10-02-2012, 09:53 AM
  4. [SOLVED] Excel 2007 : Sorting column by numbers, lowest to highest
    By elryp3000 in forum Excel General
    Replies: 4
    Last Post: 06-07-2012, 11:24 AM
  5. Proper number sorting. Lowest to Highest
    By cturtle in forum Excel General
    Replies: 5
    Last Post: 08-12-2008, 11:29 AM

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