Dynamic Array Formulas
Dynamic Array Formulas
Contents
1. SEQUENCE.............................................................................................................. 1
2. Array constants...................................................................................................... 1
3. RANDARRAY............................................................................................................ 2
4. MAKEARRAY............................................................................................................ 2
5. SORT....................................................................................................................... 4
6. UNIQUE................................................................................................................... 4
7. VSTACK and HSTACK............................................................................................... 4
8. TOCOLL & TOROW.................................................................................................. 6
9. Column-to-array transformations...........................................................................7
10. INDEX................................................................................................................... 7
11. CHOOSECOLS and CHOOSEROWS........................................................................8
12. TAKE to choose the first or last n columns or rows...............................................9
13. DROP function to remove rows or columns from range or array..........................9
14. EXPAND................................................................................................................ 9
15. Using a # reference with union operator..............................................................9
16. Dynamic Arrays with Other Excel Features.........................................................10
16.1. Tables........................................................................................................... 10
16.2. Name manager............................................................................................ 10
Using named ranges with other features...........................................................10
16.3. Charts.......................................................................................................... 10
16.4. Linked pictures............................................................................................. 11
16.5. Pivot Tables.................................................................................................. 11
16.6. Conditional formatting................................................................................. 12
16.7. Data validation............................................................................................. 12
17. How to tackle renegade functions that do not want to spill................................12
If you are writing a dynamic array formula to act on a list of data, it can
be useful to place it in an Excel table, then use structured references to
refer to the data. This is because structured references automatically
adjust as rows are added or removed from the table.
1. SEQUENCE
2. Array constants
Array constants are a component of array formulas. You create array constants
by entering a list of items and then manually surrounding the list with curly
braces:
= {1,2,3,4,5}
If you separate the items by using commas, you create a horizontal array (a
row). If you separate the items by using semicolons, you create a vertical array
(a column). To create a two-dimensional array, you delimit the items in each
row with commas, and delimit each row with semicolons.
One of the best ways to use array constants is to name them. Go to Formulas >
Defined Names > Define Name. In the Name box, type Quarter1. In the Refers
to box, enter the following constant: ={”January”, “February”, “March”}. To
refer to an element of the array you can use CHOOSE(2, Quarter1)
3. RANDARRAY
To generate an array filled with instances of a discrete variable that can take
values 1 or −1, =CHOOSE(RANDARRAY(#rows, #cols, 0, 2, TRUE), -1, 1)
4. MAKEARRAY
This function is used to construct matrices whose cells have values that depend
on the position of the cells in the matrix.
A 5×5 matrix that through element-wise multiplication changes the sign of the
off-diagonal elements of another 5×5 matrix= MAKEARRAY( 5, 5, LAMBDA(r, c,
IF(r=c, 1, -1)) )
A 5×20 matrix, with text of numbers taken from a one-column table named ‘list’
= MAKEARRAY( 5, 20, LAMBDA( r, c, INDEX(list, RANDBETWEEN(1,
ROWS(list)) ) )
6. UNIQUE
HSTACK(array1, [array2], …)
In situations when column headers or some values are missing the original
ranges, you can supply the missing data in an array constant directly to the
VSTACK or HSTACK function. For example:
Combine multiple Excel tables into one array that that updates automatically
whenever the source tables do.
=VSTACK(Table1, Table2)
In case the sources ranges contain empty cells, the returned array will have
zeros in the place of blanks. To fix this, you can nest your VSTACK or HSTACK
function in the first argument of SUBSTITUTE and tell it to replace zeros with
empty strings (“”). For example:
If the original arrays contain zero values that you want to keep and empty cells,
then substitute for empty strings rather than zeros to avoid substituting zeros
away in the returned array:
Substituting an empty string with an empty string may seem illogical, but it
does work. STACK functions display zeroes instead of blanks only for better
visualization; but if a cell of the input array is empty the corresponding cell of
the returned array is also empty. SUBSTITUTE displays empty cells as empty.
Both an empty cell and a cell containing a 0 are considered 0 for a FILTER
criterion. However only an empty cell is considered empty for a FILTER
criterion.
1 - ignore blanks
2 - ignore errors
Scan_by_column (optional)
FALSE or omitted (default) - scan the array by row from left to right.
=UNIQUE(TOCOL(A2:C7))
A #NUM error indicates that the array cannot fit into a column. A typical case is
when you refer to entire columns or rows.
9. Column-to-array transformations
To perform column-to-array transformation, use either the WRAPCOLS function to
wrap by column or the WRAPROWS function to wrap by row.
pad_with is the value with which to pad the last column if there are insufficient
items to fill it. If omitted, the missing values will be padded with #N/A (default).
Note that the INDEX function returns a how all the rows/columns if the
row/column argument is left blank.
=CHOOSECOLS(array, 2, 3, 4)
=CHOOSECOLS(array, {2,3,4}) and
=CHOOSECOLS(array, {2;3;4}) give the same result
Compute returns from an array or table of prices with the latest in the
bottom: =LN( DROP(array, 1) / DROP(array, -1) )
The DROP function in Excel removes the specified number of rows and/or
columns from the start or end of an array.
You can SORT the array by any column before dropping any row.
TAKE–DROP equivalence:
Let A be a 7-column array, then TAKE(A, ,-3) = DROP(A, ,4).
14. EXPAND
EXPAND(array, r, [c], [pad_with])
grows an array to r rows and c columns by adding cells containing pad_with or,
by default, #N/A.
=A2#:A1
=SORT(Table1)
▪ constants,
▪ arrays, and
▪ formulas.
As the name manager can hold arrays and formulas, it is more than happy to
hold dynamic array functions directly too.
The name manager is also happy holding # spill references, but we need to be
careful about their creation. While named ranges can be used with absolute or
relative cell references, to reference a spill range correctly, we must ensure the
$ symbols are used to create an absolute reference.
16.3. Charts
Even before dynamic arrays, if we wanted to use formulas such as INDEX or
OFFSET to create a dynamic range, we needed to place it in a named range.
Using a spill range directly in a chart source will result in an error. Instead, we
need to use a named range containing a spill reference.
Pivot Tables assume that the first row of our data is the header row, which is an
issue because # references would generally exclude the header. To include the
header row, we can use the following formula in a named range. It creates a
range from G2 to the bottom of the G3# spill range.
=$G$3#:$G$2
▪ Precede the input range with a + sign (or multiply it by one or add zero to
it) inside the renegade formula. (These tricks convert a range to an array.)
Example: AND won’t respond to the previous trick because, before evaluating
its arguments, it makes a single vector containing all the ranges or arrays. That
is why AND always return a single cell.
▪ Use * as an AND operator. The result will be a 0 for FALSE and 1 for TRUE.
To get a Boolean, add = 1 after the expression: = condition * condition *
condition = 1