Multiple Level Sorting Using VBA

In this article we build on the Sorting with VBA exercise.  This is about sorting on multiple levels with VBA in the most efficientway.

The following is the procedure I came up with.

Sub MultiSort() 'Sort on multiple levels with VBA
[A2:C10].Sort [a2], 1, [b2], , 1, [C2], 1
End Sub

The code can be easier to view wher the 1s stand for xlAscendingso:

[A2:C10].Sort [a2], xlAscending, [b2], , xlAscending, [C2], xlAscending

will perform the same action as the first Excel VBA example.

Of course this is based on a static range. To make the range dynamic something like the following should do.

Sub MultiSort2() 'Sort on multiple levels with VBA with a dynamic range
Dim rng As Range
Set rng=Range("A2:C" & [a65536].End(xlUp).Row)
rng.Sort [a2], 1, [b2], , 1, [C2], 1
End Sub

The attached file shows a working model using the above Excel VBA procedure.