sort-dates-within-a-date-range-using-excel-array-formula1Question: I have a list of unsorted dates. I want to extract records between two dates and I want the extracted dates sorted? A picture of the unsorted dates to the right.

Answer: See picture below.

Formula in D5:D15:

=INDEX(List, MATCH(SMALL(IF((List>=start_date)*(List<=end_date), MATCH(List+ROW(List)/1048576, SMALL(List+ROW(List)/1048576, ROW(List)-ROW(List_start)+1)), ""), ROW()-ROW(sorted_list_start)+1), MATCH(List+ROW(List)/1048576, SMALL(List+ROW(List)/1048576, ROW(List)-ROW(List_start)+1)), 0)) + CTRL + SHIFT + ENTER

Named ranges
List (A1:A10)
List_start (A1)
sorted_list_start (D5)
start_date (E1)
end_date (E2)

What is named ranges?

sort-dates-within-a-date-range-using-excel-array-formula2

How to customize the formula to your excel workbook
Change the named ranges

Download excel example file
sort-dates-within-a-date-range-using-excel-array-formula.xls
(Excel 97-2003 Workbook *.xls)

Functions used in this blog post:

IF(logical_test;[value_if:true];[value_if_false])
Checks whether a condition is met, and returns one value if TRUE, and another value if FALSE

INDEX(array,row_num,[column_num])
Returns a value or reference of the cell at the intersection of a particular row and column, in a given range

SMALL(array,k) returns the k-th smallest row number in this data set.

ROW(reference) returns the rownumber of a reference

MATCH(lookup_value;lookup_array; [match_type])
Returns the relative position of an item in an array that matches a specified value