Multiple Level Sorting Using VBAIn Microsoft Excel 2010

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.
 
img1
 
We want to sort the data by Sales Person Name & then by Country.

Following is a snapshot of manual sorting:
 
img2
 
Click on Developer tab

From Code group, select Visual Basic
 
img3
 
Click on Insert, and then Module
 
img4
 
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

 
img5
 
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.
 
img6
 
Alternatively, you can press F5 to run the code in VBA screen.

After executing the macro, we will get the following result:
 
img7
 
In this way, you can perform multiple level data sorting, easily using VBA code.
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

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.