Managing multi-layered datasets in Excel often leads to frustration when manual sorting breaks with every new data entry. While traditional analysts rely on static menu-based sorting or cumbersome custom dialog boxes, these legacy workflows lack adaptability. Transitioning to a dynamic formulaic approach grants you seamless, real-time data automation that recalculates instantly. Note that this technique requires Excel 365 or 2021 to leverage dynamic array capabilities. For instance, you can easily sort a corporate sales sheet first by "Region" alphabetically, then by "Revenue" in descending order. Below, we will detail the exact SORTBY syntax and configurations needed to automate your multi-column workflows.
For decades, Microsoft Excel users relied on the standard "Sort" dialog box located on the Data tab to organize their spreadsheets. While effective, this traditional sorting method has one massive drawback: it is static. The moment you add new records or update existing values, your sorted list becomes outdated, requiring you to repeat the multi-step sorting process all over again.
With the introduction of Dynamic Arrays in modern Excel (Excel 365, Excel 2021, and newer versions), this limitation vanished. Excel introduced two game-changing functions: SORT and SORTBY. In this comprehensive guide, we will explore how to write an Excel formula to sort multiple columns simultaneously, ensuring your data remains dynamically sorted, automated, and error-free.
To sort multiple columns dynamically, you must understand the difference between Excel's two dynamic sorting functions. While they sound identical, they operate differently under the hood.
=SORT(array, [sort_index], [sort_order], [by_col])=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)When sorting multiple columns simultaneously-especially when different columns require different sorting directions (e.g., Column A ascending, Column B descending)-the SORTBY function is the superior and most intuitive choice.
To write an effective multi-column sort formula, let's break down the arguments of the SORTBY function:
=SORTBY(array, by_array1, [sort_order1], [by_array2, sort_order2], ...)
array (Required): The range of cells or table you want to sort and display.by_array1 (Required): The first column or range to sort the primary array by. This range must have compatible dimensions with the primary array.sort_order1 (Optional): The direction of the first sort. Use 1 for Ascending (A to Z, smallest to largest) or -1 for Descending (Z to A, largest to smallest). The default is 1.by_array2, sort_order2 (Optional): Additional columns and their corresponding sort directions to resolve ties or establish a sorting hierarchy. You can add up to 126 additional sorting criteria.Let's look at a practical business scenario. Imagine you run an e-commerce store and have a dataset containing sales representative performance across different regions:
| Rep Name (Col A) | Region (Col B) | Sales ($) (Col C) |
|---|---|---|
| Sarah | East | 15,000 |
| David | West | 12,000 |
| Emma | East | 19,500 |
| John | North | 8,500 |
| Sarah | North | 22,000 |
| Emma | West | 14,000 |
Our goal is to sort this dataset dynamically based on two criteria:
Assuming your source data is in the range A2:C7, you can write the following formula in an empty cell (e.g., cell E2):
=SORTBY(A2:C7, B2:B7, 1, C2:C7, -1)
A2:C7 is our primary data range (the "array" we want to sort and display).B2:B7 is the "Region" column. The value 1 tells Excel to sort this column alphabetically from A to Z.C2:C7 is the "Sales" column. The value -1 tells Excel to sort sales in descending order when there is a tie in the Region column.When you press Enter, Excel will automatically "spill" the sorted results into the neighboring cells. If "East" contains multiple reps, Excel will arrange them so that Emma ($19,500) appears above Sarah ($15,000).
While SORTBY is generally preferred for its readability, you can actually sort multiple columns using the standard SORT function by nesting array constants in the arguments. This is an elegant, advanced trick that keeps your formula compact.
Using the same dataset above, if you want to sort by Region (Column index 2, Ascending) and then Sales (Column index 3, Descending), you can write:
=SORT(A2:C7, {2, 3}, {1, -1})
In this approach:
{2, 3} represents the column indices we want to sort.{1, -1} represents the corresponding sort orders (Ascending for index 2, Descending for index 3).This method is highly compact, but it requires your sorting columns to be physically located within the target array range, unlike SORTBY, which can utilize external helper ranges.
In real-world dashboards, you rarely want to sort an entire raw database. Often, you only want to sort a filtered subset of your data. Combining SORTBY with the FILTER function creates an incredibly powerful, dynamic reporting engine.
Let's say we only want to view sales records that are greater than $10,000, sorted by Region (A to Z) and Sales (High to Low). The formula would look like this:
=SORTBY(FILTER(A2:C7, C2:C7 > 10000), FILTER(B2:B7, C2:C7 > 10000), 1, FILTER(C2:C7, C2:C7 > 10000), -1)
Tip: To make this formula cleaner and avoid repeating the filter condition, you can leverage Excel's LET function to assign variables to your filtered dataset:
=LET(
FilteredData, FILTER(A2:C7, C2:C7 > 10000),
RegCol, INDEX(FilteredData, 0, 2),
SalesCol, INDEX(FilteredData, 0, 3),
SORTBY(FilteredData, RegCol, 1, SalesCol, -1)
)
Using LET not only optimizes calculation performance but also makes your dynamic formulas significantly easier to read, update, and debug.
When working with Dynamic Array formulas like SORTBY, you might run into a few common roadblocks. Here is how to resolve them:
This is the most common error. Excel's dynamic arrays require empty cells to output ("spill") their results. If there is text, a formula, or even a hidden space in the cells below or to the right of your formula, Excel will throw a #SPILL! error. To fix this, simply clear out the blocking data in the spill range.
The SORTBY function requires all sorting arrays (the by_array arguments) to have identical dimensions (same number of rows or columns) as your primary array. If your primary array is A2:C100 but your by_array is B2:B90, Excel will return a #VALUE! error.
If you edit your original dataset but the sorted results do not update, ensure your Workbook Calculation is set to "Automatic" instead of "Manual" (found under the Formulas tab > Calculation Options).
Transitioning from manual sorting toolbars to dynamic formula-based sorting is a massive upgrade for any Excel user. By mastering the SORTBY function, you ensure your data remains perfectly ordered, accurate, and updated in real-time, regardless of how often your source table changes. Combine it with functions like FILTER, UNIQUE, and LET to construct modern, automated dashboards that will save you hours of manual work.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.