Older Excel Versions

If you record the same sort in earlier versions of Excel, you get code like the following:

Range("B3:D9").Sort Key1:=Range("D3"), Order1:=xlAscending, Header:= _

xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal

This code uses the Sort method of the Range object. The code is much simpler than the code generated by the Sort object but is also more limited.

You can define as many sort keys as you need by adding DataField objects to the DataFields collection of the Sort object. The Range object Sort method is limited to three keys, all specified in a single execution of the Sort method. This limitation can be overcome by performing a series of sorts in the reverse order of the significance of the keys.

Naturally, this code is still supported in Excel 2007 and will be in the future. If you only need to sort on three or fewer keys, it gives you a simpler alternative to the Sort object.

0 0

Post a comment