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 )
Worksheet: Sorting
Row\Col R S T U V W 22Food Product Was S22 Kcal Was U22 Salt Was W22 23Crisps Was S23 500Was U23 0.7Was W23 24Beer Was S24 200Was U24 0.1Was W24 25Wine Was S25 150Was U25 0.15Was W25 26Beer Was S26 200Was U26 0.07Was W26 27beer Was S27 220Was U27 0.2Was W27 28Beer Was S28 210Was U28 0.06Was W28 29Wine Was S29 160Was U29 0.04Was W29 30wiNe Was S30 150Was U30 0.03Was W30 31Crisps Was S31 502Was U31 2Was W31 32Onion Ringes Was S32 480Was U32 1Was W32 33Onion Ringes Was S33 490Was U33 1.5Was W33 34Crisps Was S34 502Was U34 1.5Was W34 35CRISPS Was S35 500Was U35 1.1Was W35 36Wine Was S36 170Was U36 0.1Was W36 37Crisps Was S37 500Was U37 3Was W37
Here is a demo Calling test routine
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 lineCode: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 '




Reply With Quote
Bookmarks