Take an example,
A list of Foods, their name in first column and a few other things like calories(Kcal) and Salt content in other columns
First I want to sort to group similar products (based on alphabetical order, but ascending or descending is not important) - This will be sorting on column 1 values
Within similar food types, I want to list them in an order of how healthy they might be, ( or at least in the order of least unhealthy ) .
Most important would be order starting with lowest Kcal.
After that for similar products with similar Kcal , we would consider the minimum salt content as likely to be the less unhealthy.
This might be our list
_____ ( Using Excel 2007 32 bit )
Row\Col |
R |
S |
T |
U |
V |
W |
22 |
Food Product |
Was S22 |
Kcal |
Was U22 |
Salt |
Was W22 |
23 |
Crisps |
Was S23 |
500 |
Was U23 |
0.7 |
Was W23 |
24 |
Beer |
Was S24 |
200 |
Was U24 |
0.1 |
Was W24 |
25 |
Wine |
Was S25 |
150 |
Was U25 |
0.15 |
Was W25 |
26 |
Beer |
Was S26 |
200 |
Was U26 |
0.07 |
Was W26 |
27 |
beer |
Was S27 |
220 |
Was U27 |
0.2 |
Was W27 |
28 |
Beer |
Was S28 |
210 |
Was U28 |
0.06 |
Was W28 |
29 |
Wine |
Was S29 |
160 |
Was U29 |
0.04 |
Was W29 |
30 |
wiNe |
Was S30 |
150 |
Was U30 |
0.03 |
Was W30 |
31 |
Crisps |
Was S31 |
502 |
Was U31 |
2 |
Was W31 |
32 |
Onion Ringes |
Was S32 |
480 |
Was U32 |
1 |
Was W32 |
33 |
Onion Ringes |
Was S33 |
490 |
Was U33 |
1.5 |
Was W33 |
34 |
Crisps |
Was S34 |
502 |
Was U34 |
1.5 |
Was W34 |
35 |
CRISPS |
Was S35 |
500 |
Was U35 |
1.1 |
Was W35 |
36 |
Wine |
Was S36 |
170 |
Was U36 |
0.1 |
Was W36 |
37 |
Crisps |
Was S37 |
500 |
Was U37 |
3 |
Was W37 |
Worksheet: Sorting
Here is a demo Calling test routine
Code:
Sub TestieSimpleArraySort6()
Rem 0 test data, worksheets info
Dim WsS As Worksheet: Set WsS = ThisWorkbook.Worksheets("Sorting")
Dim RngToSort As Range: Set RngToSort = WsS.Range("R23:W37")
' Set RngToSort = Selection ' ' Selection.JPG : https://imgur.com/HnCdBt8
Dim arrTS() As Variant: Let arrTS() = RngToSort.Value ' We would have to use .Value for a range capture of this sort because .Value returns a field of Variant types. But also at this stage we want to preserve string and number types
' Call SimpleArraySort6(1, arrTS(), " 1 2 3 4 5 ", " 1 Asc 2 Asc 3 Asc")
Dim cnt As Long, strIndcs As String: Let strIndcs = " "
For cnt = 1 To RngToSort.Rows.Count
Let strIndcs = strIndcs & cnt & " "
Next cnt
Debug.Print strIndcs ' For 5 rows , for example we will have " 1 2 3 4 5 " , for 15 rows " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 "
Call SimpleArraySort6(1, arrTS(), strIndcs, " 1 Desc 3 Asc 5 Asc")
Rem 2 Output for easy of demo
' 2a
RngToSort.Offset(0, RngToSort.Columns.Count).Clear
Let RngToSort.Offset(0, RngToSort.Columns.Count).Value = arrTS()
Let RngToSort.Offset(0, RngToSort.Columns.Count).Interior.Color = vbYellow
' 2b VBA Range.Sort Method equivalent
Dim TestRngSrt As Range: Set TestRngSrt = RngToSort.Offset(0, RngToSort.Columns.Count * 2)
TestRngSrt.Clear
Let TestRngSrt.Value = RngToSort.Value
TestRngSrt.Sort Key1:=TestRngSrt.Columns("A:A"), order1:=xlDescending, Key2:=TestRngSrt.Columns("C:C"), order2:=xlAscending, Key3:=TestRngSrt.Columns("E:E"), order3:=xlAscending
TestRngSrt.Interior.Color = vbGreen
End Sub
'
That above routine uses the test range R23:W37 above and feeds that to the main recursion routine below in the next post. The demo routine also does the VBA Range.Sort equivalent code line
Bookmarks