So I have a table of values:
Col A Col B Col C
Row 1 23410 7000.00 6939.45
Row 2 23397 15000.00 11244.51
Row 3 27060 4000.00 3524.78
I need to be able to sort the values (up to 10 eventually) using an multidimensional array by Column B. The code will eventually be in a UserForm (and thus cannot rely on a spreadsheet sort).
The following code works on two conditions: 1. Only 3 rows of data (need up to 10) & 2. If it is in the order as shown (needs to be any order)
Option Explicit
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, 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)
Else
MaxSort(1, x) = sortValue(1, x)
MaxSort(2, x) = sortValue(2, x)
MaxSort(3, x) = sortValue(3, x)
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
Bookmarks