Table Of Contents
Excel VBA Sort Range
Sorting a range in VBA is done by the range.sort method. It is a property of the range method with which a user can sort a range in order. The arguments for this function are: Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3. All the arguments for this function are optional.
As part of data organizing or data structuring, it is important to sort the data and make it organized. A similar thing is also available with VBA. A common question about new learners of VBA is how we can use this sort of option as part of VBA automation. This article guides you through the VBA Sort range in detail.
With Excel, we are all familiar with the sort option available under the DATA tab.
Sort Option in VBA
To use the sort option, first, we need to decide what our data range is and mention the same data range by using the RANGE object in VBA. Then, only we can access the "Sort" option in VBA. For example, assuming my data range is from A1 to D10, we can provide the data range as follows.
Code:
Sub Sort_Range_Example() Range ("A1:D10") End Sub
Now, put a dot and select the "SORT" method.
Code:
Sub Sort_Range_Example() Range("A1:D10").Sort End Sub
Below is the syntax of the SORT method of range. Though syntax has different arguments, we do not need all of them for our VBA coding. So, we need only a few elements.
: In the data range we are sorting, we need to specify which column we need to sort. For example, in the data range of A1: D10, if we want to sort the data based on column B, then will be Range("B1").
: The mentioned column in the argument should sort in what order? We can choose two options here: "xlAscending" or "xlDescending."
Header: The mentioned data range has headers or not? If yes, we can supply "xlYes," or else we can supply "xlNo."
Example of Sort Range in VBA
Let us take the example of the Excel VBA sort range to understand this better.
Look at the below data structure.
We have data from A1 to E17. First, we will sort the data based on "Country-wise." Then, follow the below steps to write the code to sort the data.
Step 1: Start the excel macro procedure.
Code:
Sub Sort_Range_Example() End Sub
Step 2: First, mention the data range using the RANGE object.
Code:
Sub Sort_Range_Example() Range ("A1:E17") End Sub
Step 3: Now, choose the "Sort" method of the Range object.
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort End Sub
Step 4: Since we are sorting the data based on "Country-wise," our Key 1 argument column will be Range ("B1").
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort Key1:=Range("B1"), End Sub
Step 5: Once we mention the required column, we need to mention in what order we need to sort the data, and "Order1" will be the "xlAscending" order.
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort Key1:=Range("B1"), Order1:=xlAscending, End Sub
Step 6: Our data has headers so that that header will be "xlYes."
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes End Sub
We have mentioned all the elements which are required to sort the data. Then, execute the code by pressing the F5 function and key and see the result.
Output:
The data has been sorted based on country names from A to Z.
Now, assume we need to sort the data based on country-wise. Also, we need to sort the "Gross Sales" from highest to lowest. In such cases, we also need to use Key2 and Order2 arguments.
After mentioning Key1 and Order1, let us choose Key2.
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort Key1:=Range("B1"), Order1:=xlAscending,Key2:= End Sub
Since we are sorting the data based on the "Gross Sales" column, our Key2 column name will be Range("D1").
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort Key1:=Range("B1"), Order1:=xlAscending,Key2:=Range("D1"), End Sub
Once the Key2 is specified, we need to decide on the sorting pattern of whether it is ascending order or descending order in the Order2 argument. Since we are sorting the sales value from largest to smallest, we can choose the "xlDescending" order.
Code:
Sub Sort_Range_Example() Range("A1:E17").Sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("D1"), Order2:=xlDescending, Header:=xlYes End Sub
After that, mention the header argument as "xlYes." Now, run the code and see what happens.
Output:
Previously, it was sort based only on "Country-wise." But this time, it sorted based on "Country-wise" first and then on "Gross Sales" from highest to lowest.
Like this, we can use the "Sort" method in VBA to organize the data.
Things to Remember about Excel VBA Sort Range
- The sort is a method available in VBA. To access this method, we need to specify the range of cells we will sort.
- If the data range includes headers, we need to choose the header option as "xlYes." If not, we can choose "xlNo."