The material in this post is generalized in another post. Also, if you are interested in this post, you may be interested in my book Data Analysis Using SQL and Excel.
Consider a table with two columns:
The sorted table would be automatically calculated as:
If the first value were changed to 5, then the sorted table would automatically recalculate as:
There are two typical approaches to sorting cells in Excel. The first is to select a region and to sort it using menu options. This does not work when the cells are protected, part of a pivot table, and sometimes when they are calculated. This might also be a bad idea when the data is copied from another location or loaded by accessing a database.
A common alternative is to resort to writing a macro. However, Visual Basic macros are beyond the capabilities of even many experienced Excel users.
The approach described here is much simpler, since it only uses formulas. I should mention that the method described in this post only works for numeric data that has no duplicates. I will remedy that in the next post, where the ideas are extended both to data with duplicates and to character data.
Three Excel functions are the key to the idea: RANK(), MATCH(), and OFFSET(). The first function ranks numbers in a list. The second allows us to use this info to sort the list.
The following shows the effect of the RANK() function:
The function itself looks like:
|15||B||=RANK(C5, $C$5:$C$8, 1)|
|14||D||=RANK(C6, $C$5:$C$8, 1)|
|13||C||=RANK(C7, $C$5:$C$8, 1)|
|11||A||=RANK(C8, $C$5:$C$8, 1)|
The first argument is the value to be ranked. The cell C5 contains the value "15". The second argument is the range to use for h the ranking -- these are all the values in the cell. And the third is the direction, which means the lowest values get the lowest rankings. In this case, "15" is the largest value of four, so its rank is 4.
The following shows the formulas for the table:
|1||=OFFSET(C$4, MATCH(C11, $E$5:$E$8, 0), 0)||=OFFSET(D$4, MATCH(C11, $E$5:$E$8, 0), 0)|
|2||=OFFSET(C$4, MATCH(C12, $E$5:$E$8, 0), 0)||=OFFSET(D$4, MATCH(C12, $E$5:$E$8, 0), 0)|
|3||=OFFSET(C$4, MATCH(C13, $E$5:$E$8, 0), 0)||=OFFSET(D$4, MATCH(C13, $E$5:$E$8, 0), 0)|
|4||=OFFSET(C$4, MATCH(C14, $E$5:$E$8, 0), 0)||=OFFSET(D$4, MATCH(C14, $E$5:$E$8, 0), 0)|
The first column is the desired ranking column. This is simply the numbers starting at 1 and incrementing by 1. The function MATCH(C11, $E$5:$E$8, 0) simply looks up the ranks in the column of calculated ranks. So, the value in C11 is "1". In the previous column, this is the fourth value. The OFFSET() function then finds the fourth value in the C column for the value and the fourth value in the D column for the label.
The result is that the sorted table is tied to the original table by formulas, so changing values in the original table will result in recalculating the sorted table, automatically.
The overall approach is simple to describe. First, we need to calculate the ranking of each row in the original table based on the column that we want to sort. This ranking takes on the values 1 to N fo rthe values in the table. Then, we create a new sorted table that has the rankings in order. This table looks up the appropriate row for each ranking using the MATCH() function. Finally, the OFFSET() function is used to lookup the appropriate values from the appropriate row. The result is a table that is sorted with a "live" connection to another table.