0% found this document useful (0 votes)
10 views

Dynamic Array Formulas

New dynamic array formulas that spill

Uploaded by

Rodolfo Oviedo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Dynamic Array Formulas

New dynamic array formulas that spill

Uploaded by

Rodolfo Oviedo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Dynamic Array Functions

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

Dynamic array functions can even be used to produce simple scenario


managers. These are more convenient than data tables since all the results are
live all the time plus they are about 1,000 times faster.

Dynamic array formulas


 If function do not spill, try preceding the spill range with a plus (+) sign.

 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.

 Spilled array formulas are not supported in Excel tables themselves, so


you should place them in the grid outside of the Table. Tables are best
suited to holding rows and columns of independent data.

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}

= {”January”, “February”, “March”}

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.

=SEQUENCE(1,5) is the same as ={1,2,3,4,5}

=SEQUENCE(5) is the same as ={1;2;3;4;5}

=SEQUENCE(3,4) is the same as ={1,2,3,4; 5,6,7,8; 9,10,11,12}

=SEQUENCE(rows, columns, start, step), then =SEQUENCE(1, 5, 3, 1) is the


same as ={3,4,5,6,7}

Array constants can’t contain references, functions, formulas, or additional


arrays. In other words, they can contain only text or numbers that are
separated by commas or 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.

Examples written directly in Word without trying them in Excel:

A 5×5 identity matrix = MAKEARRAY( 5, 5, LAMBDA(r, c, IF(r=c, 1, 0)) )

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 4×4 flipping matrix = MAKEARRAY( 5, 5, LAMBDA(r, c, IF(r+c=5, 1,)) ).


Flipping a matrix upside down can be done more efficiently with
INDEX( matrix, SEQUENCE(ROWS(matrix), 1, ROWS(matrix), -1), SEQUENCE(1,
COLS(matrix)) )

A 9×9 diagonal matrix with the (first) elements of a column vector


= MAKEARRAY( 9, 9, LAMBDA( r, c, IF(r=c, INDEX(vector, r), 0) ) ). To convert
this matrix back to a column vector, post‍‍multiply it by a 9-element columns
vector of ones, which can be constructed with SEQUENCE(9, 1, 1, 0)

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)) ) )

A 20-element column whose cells have values −1 or 1 at random with equal


probability =MAKEARRAY(20, 1, LAMBDA(r,c, CHOOSE(RANDBETWEEN(1,2), -1,
1))). More concisely:
=CHOOSE(RANDARRAY(20,1,1,2,TRUE), -1, 1)
5. SORT

6. UNIQUE

7. VSTACK and HSTACK


VSTACK(array1, [array2], …)

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:

=VSTACK( {”Item”, “Color”, “Size”}, A2:C6, A10:C14 )

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:

=SUBSTITUTE(VSTACK(A3:B7, A12:B16), 0, “”)

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:

=SUBSTITUTE(VSTACK(A3:B7, A12:B16), “”, “”)

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.

SORT FILTER UNIQUE

8. TOCOLL & TOROW


converts an array or range of cells into a single column

TOCOL(array, [ignore], [scan_by_column])

Array (required) - an array or range to transform into a column. Actually, a


range is converted to an array as an intermediate step.
Ignore (optional)

 0 or omitted (default) - keep all values

 1 - ignore blanks

 2 - ignore errors

 3 - ignore blanks and errors

Scan_by_column (optional)

 FALSE or omitted (default) - scan the array by row from left to right.

 TRUE - scan the array by column from top to bottom.

=TOCOL(A2:C5, ,TRUE) proceses the array by column.

UNIQUE function cannot extract unique values from a multi-column array.


Solution:

=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.

WRAPCOLS(vector, n, [pad_with]) wraps the vector in side-by-side columns


or n elements each.

WRAPROWS(vector, n, [pad_with]) wraps the vector in side-by-side rows or n


elements each.

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).

A #VALUE error occurs if the vector argument is not a one-dimensional array.

A #NUM error occurs if the wrap_count value is 0 or negative number.


10. INDEX
INDEX can be used to slice part of a dynamic array. In the example below The
SORT function is applied to cells B3-E10, in descending order based on column
2. INDEX is returning rows 1, 3, 5, and 7 and columns 1 and 4 from the array.

Note that the INDEX function returns a how all the rows/columns if the
row/column argument is left blank.

11. CHOOSECOLS and CHOOSEROWS


=CHOOSECOLS(array, 1, 3, −1, −2) chooses the first, third and the last two
columns

=CHOOSECOLS(array, 2, 3, 4)
=CHOOSECOLS(array, {2,3,4}) and
=CHOOSECOLS(array, {2;3;4}) give the same result

=CHOOSECOLS(array, G4, H4, I4) and


=CHOOSECOLS(array, G4:I4) give the same result

=CHOOSECOLS(array, SEQUENCE(ROUNDUP( COLUMNS(array)/2, 0), 1, 1,


2))
gets the odd columns
=CHOOSECOLS(array, SEQUENCE(ROUNDDOWN(COLUMNS(array)/2, 0), 1, 2,
2))
gets the even columns

=CHOOSECOLS(array, SEQUENCE(COLUMNS(array)) *-1) reverses the order of


columns

Returns #VALUE! if the absolute value of any col_num argument is zero or


greater than the total number of columns in the referred array.

12. TAKE to choose the first or last n columns or


rows
TAKE(array, r, [c]) extracts the cells contained in the first r rows and the
first c columns. If r or c is negative, the function chooses the last rows or
columns.

TAKE(array, , 3) is equivalent to CHOOSECOLS(array, 1, 2, 3).

13. DROP function to remove rows or columns


from range or array
Use cases:

 Compute returns from an array or table of prices with the latest in the
bottom: =LN( DROP(array, 1) / DROP(array, -1) )

 Pull data without headers, footers, totals row or totals column.

The DROP function in Excel removes the specified number of rows and/or
columns from the start or end of an array.

DROP(array, rows, [columns])

Omitted rows default to zero.

You can SORT the array by any column before dropping any row.

#CALC! error will indicate that there is nothing to return.

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.

15. Using a # reference with union operator


Suppose we have a spill range starting at cell A2, and we want to include a
header row into that spill range. We could use the following formula:

=A2#:A1

Or we can use VSTACK.

16. Dynamic Arrays with Other Excel Features


16.1. Tables
Dynamic arrays can refer to tables:

=SORT(Table1)

16.2. Name manager


The name manager can hold:

▪ 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.

Using named ranges with other features


As we go through the remainder of this post, you will see that named ranges
are key to using dynamic arrays with many other features.
Even though a spill range is recognized as a range for formulas, it appears that
Excel needs to perform some background processing to work out how big the
range is. The name manager can perform calculations, so it can trigger this
processing. Features that don’t contain any calculations can use named ranges
as their source to handle this part for them. We’ll look at this further in the
sections below.

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.

16.4. Linked pictures


The screenshot below shows a picture linked to a named range called
NamedRange_LinkedPicture.
When the results change, so does the picture; it grows or shrinks with the size
of the spill range.

The formula in the name manager by the name of NamedRange_LinkedPicture


(shown below) joins two spill ranges together into a single named range:

=’Linked Picture’!$G$3#:’Linked Picture’!$H$3#

16.5. Pivot Tables


Initially, Pivot Tables appear to be happy using a # reference as a source. But
don’t be fooled! As soon as we click OK, Excel converts the # reference to a
standard static range and then creates the Pivot Table. Instead, we can turn to
named ranges once again.

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

16.6. Conditional formatting


Same as Pivot Tables

16.7. Data validation


Data validation lists perform calculations; therefore, they work exceptionally
well with the # referencing system by themselves. The data must still be in the
right shape to work with data valuation, so it must be a single row or column. If
we want to use a named range, that will work too.

17. How to tackle renegade functions that do not


want to spill

Example: EOMONTH(dates, months ahead) does not respond to an input range


of dates.

▪ 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

You might also like