# How to Sort Multiple Columns of a Table with Excel VBA (2 Methods)

In this article, Iâ€™ll show you how you can sort multiple columns of a table using VBA in Excel. Youâ€™ll learn to sort all the columns of a table based on both a single column and multiple columns.

Sort Multiple Columns of a Table with Excel VBA (Quick View)

``````Sub Sort_Table_Based_on_Multiple_Columns()

Worksheets("Sheet1").Range("Table1").Sort Key1:=Range("Table1[Joining Date]"), _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Order1:=xlAscending, _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  Â Â Â Â Â Â Â Â Header:=xlYes, _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Key2:=Range("Table1[Salary]"), _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Order2:=xlDescending

End Sub``` ```

## 2 Suitable Ways to Sort Multiple Columns of a Table with Excel VBA

Here weâ€™ve got a table called Table1 in a worksheet called Sheet1 of an Excel workbook that contains three columns namely Employee Name, Joining Date, and Salary.

Our objective today is to sort multiple columns of this table with Excel VBA.

### 1. Sort Multiple Columns of a Table based on a Single Column with Excel VBA

First of all, weâ€™ll sort all the columns of this table based on a single column.

Letâ€™s try to sort the whole table (Table1) of Sheet1 according to the ascending order of the joining dates of the employees (Seniority-wise).

â§­ Overview of the Code:

Before going to the main code, letâ€™s have an overview of the code first. Weâ€™ll use the Sort method of VBA for this purpose.

The Sort method of VBA takes 3 parameters.

â§ª Key: The column based on which sorting will be made. Here itâ€™s Table1[Joining Date].

â§ª Order: The order in which the Key column will be sorted. Here itâ€™s xlAscending.

â§ª Header: Represents whether the table has headers or not. Here itâ€™s xlYes (As the table has headers).

â§­ Special Note:

The Key parameter and the Order parameter can be more than one in number (in case the table is sorted based on multiple columns). So we have to number them in the order, like Key1, Order1, Key2, Order2, etc.

Here we have only one Key and one Order. So weâ€™ll use only Key1 and Order1.

â§­ VBA Code:

Now itâ€™s the time for the code. The complete VBA code will be:

``````Sub Sort_Table_Based_on_Single_Column()

Worksheets("Sheet1").Range("Table1").Sort Key1:=Range("Table1[Joining Date]"), _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Order1:=xlAscending, _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Header:=xlYes

End Sub``````

â§­ Output:

Run the code (Obviously after changing the inputs according to your need). Youâ€™ll find the whole table sorted according to the ascending order of the joining dates of the employees.

### 2. Sort Multiple Columns of a Table based on Multiple Columns with Excel VBA

In the previous method, we sorted the table based on the joining dates only.

But on the joining dates, there are some repetitions. That is, the joining dates of a few employees are the same. This time when such a case occurs, weâ€™ll sort them in the descending order of their salaries.

Therefore, the summary is, first, weâ€™ll sort based on the joining dates (ascending order), then based on salaries (descending order).

â§­ Overview of the Code:

Weâ€™ll again use the Sort method of VBA. But this time, there will be 2 Key parameters (Key1:= Table1[Joining Date] and Key2:= Table1[Salary]) and 2 Order parameters (Order1:= xlAscending and Order2:= xlDescending)

â§­ VBA Code:

Therefore, the VBA code will be:

``````Sub Sort_Table_Based_on_Multiple_Columns()

Worksheets("Sheet1").Range("Table1").Sort Key1:=Range("Table1[Joining Date]"), _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Order1:=xlAscending, _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  Â Â Â Â Â Â Â Â Header:=xlYes, _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Key2:=Range("Table1[Salary]"), _
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Order2:=xlDescending

End Sub``````

â§­ Output:

Run the code (After changing the inputs). This time youâ€™ll find the whole table sorted according to the ascending order of the joining dates of the employees first, then according to the descending order of the salaries.

## Things to Remember

Here Iâ€™ve used the Sort method of VBA to sort the table. But there is a built-in function in Excel called the Sort function that we can use directly to sort any range (Available in Office 365 only).

## Conclusion

So these are the methods to sort multiple columns of a table using VBA in Excel. Do you have any questions? Feel free to ask us. And donâ€™t forget to visit our site ExcelDemy for more posts and updates.

## Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

1 Comment
1. Thank you for such brilliant code.

Advanced Excel Exercises with Solutions PDF