Table of Contents
Using VBA (Visual Basic for Applications) in Microsoft Excel, you can easily sort a sheet by multiple columns. This feature allows you to arrange data in a specific order by specifying multiple sorting criteria. By using VBA code, you can customize the sorting process to fit your specific needs, such as sorting in ascending or descending order, sorting by multiple columns in a specific sequence, and even adding additional criteria for more precise sorting. This function can be particularly useful for organizing large amounts of data and making it easier to analyze and interpret. With the ability to sort by multiple columns, VBA provides a versatile and efficient tool for managing and organizing data in Excel.
VBA: Sort Sheet by Multiple Columns
You can use the following basic syntax to sort an Excel sheet by multiple columns using VBA:
Sub SortMultipleColumns()
Range("A1:B11").Sort Key1:=Range("A1"), Order1:=xlAscending, _
Key2:=Range("B1"), Order2:=xlDescending, _
Header:=xlYes
End SubThis particular example sorts the cells in the range A1:B11 first by the values in column A ascending, then by the values in column B descending.
Note that Header:=xlYes specifies that the first row should be treated as a header row.
The following example shows how to use this syntax in practice.
Example: Sort Excel Sheet by Multiple Columns Using VBA
Suppose we have the following dataset in Excel that contains information about various basketball players:

Suppose we would like to sort the dataset in the following manner:
- First, sort by the values in the Team column ascending (A to Z)
- Then, sort by the values in the Points column descending (largest to smallest)
We can create the following macro to do so:
Sub SortMultipleColumns()
Range("A1:B11").Sort Key1:=Range("A1"), Order1:=xlAscending, _
Key2:=Range("B1"), Order2:=xlDescending, _
Header:=xlYes
End SubWhen we run this macro, we receive the following output:

The rows are now sorted by team name in ascending order and points in descending order.
Note #1: In this example we sorted by two columns. However, you can specify as many Keys as you would like to sort by even more columns.
Note #2: You can find the complete documentation for the VBA Sort method .
Cite this article
stats writer (2024). How can I use VBA to sort a sheet by multiple columns?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-sort-a-sheet-by-multiple-columns/
stats writer. "How can I use VBA to sort a sheet by multiple columns?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-sort-a-sheet-by-multiple-columns/.
stats writer. "How can I use VBA to sort a sheet by multiple columns?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-sort-a-sheet-by-multiple-columns/.
stats writer (2024) 'How can I use VBA to sort a sheet by multiple columns?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-sort-a-sheet-by-multiple-columns/.
[1] stats writer, "How can I use VBA to sort a sheet by multiple columns?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use VBA to sort a sheet by multiple columns?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
