In this article, you will learn how to do multiple-level sorting through VBA code.
Data sorting is the most used feature of Excel; used on a daily basis. With multiple level data sorting, you can make the data easier to understand.
Let us take an example:
We have Sales Report that contains Sales Person, Country & Sales Amount.
We want to sort the data by Sales Person Name & then by Country.
Following is a snapshot of manual sorting:
Click on Developer tab
From Code group, select Visual Basic
Click on Insert, and then Module
This will create a new module.
Enter the following code in the Module
Sub Multiple_Data_Sorting()
Sheets("sheet1").Range("A1:C" & Sheets("sheet1").Range("A1").End(xlDown).Row).Sort _
key1:=Sheets("sheet1").Range("A:A"), order1:=xlAscending, _
key2:=Sheets("sheet1").Range("B:B"), order2:=xlAscending, _
Header:=xlYes
End Sub
The 1ST level SORT ON Sales Person (Column A)
The 2ND level SORT ON Country (Column B)
Press ALT + F8 shortcut key for opening Macro window, and then select the macro.
Alternatively, you can press F5 to run the code in VBA screen.
After executing the macro, we will get the following result:
In this way, you can perform multiple level data sorting, easily using VBA code.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.