Explanation
Note: this formula is the set-up for a formula that can extract and display data using a predefined sort order in a helper column. One example here .
The core of this formula is the RANK function, which is used to generate a rank of sales values, where the highest number is ranked #1:
=RANK(C5,sales)
Here, RANK uses the named range “sales” (C5:C11) for convenience. By default, RANK will assign 1 to the highest value, 2 to the second highest value, and so on. This works perfectly as long as numeric values are unique. However, to handle numeric values which contain duplicates, we need to use the COUNTIF function to break ties. This is done by adding the result of this snippet to the value returned by RANK:
COUNTIF($C$5:C5,C5)-1
Notice the range is entered as a mixed reference that will expand as the formula is copied down the table. As written, this reference will include the current row, so we subtract 1 to “zero out” the first occurrence. This means the expression will return zero for each numeric value until a duplicate is encountered. At the second instance, the expression will return 1, at the third instance, it will return 2, and so on. This effectively breaks ties, and allows the formula to generate a sequential list of numbers with no gaps.
Once the formula is in place, data can be sorted by the helper column . It can also be retrieved with INDEX using the values in the helper column.
Note: This formula is adapted from an example in the excellent book Control + Shift + Enter , by Mike Girvin .
Explanation
At the core, this formula builds a level 1 and level 2 number and concatenates the two numbers together with a period (".") as a separator. The result is a value like “1.1”. The “level 1” number is generated with COUNTA like this:
=COUNTA($B$5:B5)
Note the range is an expanding reference , so it will expand as it is copied down the column.
The “level 2” number is generated with this code:
IF(B5<>"",1,MID(D4,FIND(".",D4)+1,LEN(D4))+1)
Here, the IF function is used to check the contents of B5. If B5 is not blank, it means we have a new level 1 heading and IF returns 1. In other words, every time we have a new level 1 entry, we restart level 2 numbering at 1.
If the B5 is blank we need to increment the level 2 number using the value in the cell above. This is a bit tricky, because the outline number is a text string, not a number. That means we need to extract the value with a text function before we can increment. To do this, we use the MID function to extract all text to the right of the period ("."), which we locate with the FIND function:
MID(D4,FIND(".",D4)+1,LEN(D4))+1
The LEN function is used as a simple way to guarantee all characters after the period are extracted. Notice we then add 1 directly to the result, which is still text. This math operation causes Excel to coerce the text to a number, so the result is an incremented number. Finally, the level 1 and level 2 numbers are concatenated together with a period (".") as a separator.