Formula Magic With Dynamic Arrays in Excel 365
Formula Magic With Dynamic Arrays in Excel 365
Dynamic Arrays
in Excel 365
Mark Proctor
https://exceloffthegrid.com
Copyright
Copyright © Excel Off The Grid, part of Twenty Seven Solutions Limited
All rights reserved. This publication is protected by copyright. No part of this publication may be
reproduced, stored in a retrieval system or transmitted in any form or by any means, except as
permitted by the copyright holder.
Because of the possibility of human or mechanical error, the copyright holder does not guarantee the
accuracy, adequacy or completeness of any information. The copyright holder accepts no liability for
any inaccuracy, error of omission, or for the results obtained, regardless of cause from the use of any
information.
The copyright holder does not warrant or guarantee that the information contained in the work will
meet your requirement or its fitness for a particular purpose.
Contents
About this book ............................................................................................................ 1
Understanding dynamic arrays .................................................................................... 3
Overview of dynamic arrays..................................................................................... 4
Dynamic array formulas ........................................................................................... 6
Spilling ...................................................................................................................... 9
# references ........................................................................................................... 11
Constant arrays ...................................................................................................... 12
The @ symbol......................................................................................................... 13
What next? ............................................................................................................. 14
The UNIQUE function ................................................................................................. 15
Arguments of the UNIQUE function....................................................................... 15
Example 1 – The difference between unique and distinct .................................... 16
Example 2 – UNIQUE across 2 columns ................................................................. 17
Example 3 – Using UNIQUE across columns .......................................................... 19
Example 4 – Combining UNIQUE with SORT in a data validation list..................... 21
Example 5 – Simple formula based Pivot Report ................................................... 22
The SORT function ...................................................................................................... 24
Arguments of the SORT function ........................................................................... 24
Example 1 – SORT returns an array of rows and columns ..................................... 24
Example 2 – SORT by another column in descending order .................................. 25
Example 3 – Using SORT to return the top 5 values .............................................. 26
Example 4 – Combining FILTER and SORT .............................................................. 27
Example 5 – SORT on multiple columns................................................................. 28
The SORTBY function.................................................................................................. 29
Arguments of the SORTBY function ....................................................................... 29
Example 1 – The sort column does not need to be in the array ............................ 30
Example 2 – SORTBY retains the Excel table column names ................................. 30
Example 3 – Using SORTBY with multiple columns ................................................ 31
Example 4 – Returning columns in any order when using SORTBY ....................... 32
Example 5 – Combining FILTER and SORTBY .......................................................... 33
Example 6 – Restrict the values returned by SORTBY ............................................ 35
The FILTER function .................................................................................................... 36
Arguments of the FILTER function ......................................................................... 36
Example 1 – FILTER returns an array of rows and columns ................................... 36
Example 2 – #CALC! error caused by the FILTER function ..................................... 37
Example 3 – Using FILTER with multiple criteria. ................................................... 39
Example 4 – Using FILTER with other functions ..................................................... 40
The SEQUENCE function ............................................................................................. 42
Arguments of the SEQUENCE function .................................................................. 42
Example 1 – Basic usage......................................................................................... 42
Example 2 – Using SEQUENCE inside other functions ........................................... 44
Example 3 – SEQUENCE row / column order ......................................................... 45
Example 4 – SEQUENCE with INDEX....................................................................... 45
Example 5 – SEQUENCE in random order .............................................................. 46
The RANDARRAY function .......................................................................................... 48
Arguments of the RANDARRAY function................................................................ 48
Example 1 – Basic usage......................................................................................... 49
Example 2 – Using RANDARRAY with SORTBY ....................................................... 50
Example 3 – Using all the arguments of RANDARRAY ........................................... 51
Example 4 – Replacing RAND and RANDBETWEEN ................................................ 51
Using dynamic arrays with other features ................................................................. 53
Overview ................................................................................................................ 53
Excel tables ............................................................................................................. 55
Name manager ....................................................................................................... 56
Charts ..................................................................................................................... 58
Linked pictures ....................................................................................................... 59
PivotTables ............................................................................................................. 61
Conditional formatting ........................................................................................... 64
Data validation ....................................................................................................... 65
Advanced formula techniques ................................................................................... 66
Single formula Vs. cascading formula methodology .............................................. 66
Useful supporting functions ................................................................................... 69
Using # references with the range operator .......................................................... 71
The final word ............................................................................................................ 76
About this book
The formulas and techniques contained in this book are illustrations of what can be
achieved with dynamic arrays. In most circumstances, the formulas will need to be
customized to your specific needs.
Of all the chapters contained in the book, most are aimed at intermediate to advanced
Excel users. However, some concepts contained in the following chapters may be
stretching for many:
It is impossible to write a book which matches every reader’s skill level. Therefore, if there
are sections you do not understand, then just move on. Once you’ve progressed along
your Excel journey, these are the sections you can return to.
While it is possible to just read this book, the best way to learn is to work through the
examples using support files, and by applying the principles directly to your work.
Support files
All the examples are available in the support files, which can be downloaded here:
https://exceloffthegrid.com/dynamic-arrays-book
Found an error?
Whilst I try to create accurate examples and write accurate text, I can (and often do) make
mistakes. Please backup copies of your files before using any sections in this book. Backing
up ensures that if anything goes seriously wrong, you can revert to a previous working
version.
1
If you do find errors, please let me know.
2
Understanding dynamic arrays
Excel has changed… like seriously, changed. Every time we used Excel in the past, we
accepted a simple operating rule; 1 formula, one cell. Even with advanced formulas, it was
still necessary to have one cell for each calculation. But this has changed; Excel now allows
a single formula to fill multiple cells. This is possible because of Excel’s new calculation
engine, which allows dynamic arrays.
The term ‘dynamic arrays’ sounds complicated, but once you understand it, you’ll
appreciate the simplicity and power. In this chapter we will cover the key concepts, so
that you understand how the apply dynamic arrays.
Microsoft has already confirmed this new functionality will not be available in Excel 2019
or prior versions.
So, if you want to use dynamic arrays it’s either time to upgrade to a Microsoft 365 license
or try them out on Excel Online.
When Microsoft announced dynamic arrays, they also introduced 6 new functions. All of
which make use of this new calculation engine:
=B2:B6
OK, it’s probably not a formula you’ve ever used, but it’s simple enough to help describe
the impact of dynamic arrays.
In the old versions of Excel, only one cell result could be returned. Depending on where
the formula above was entered there are two possible outcomes:
• If the formula was in line with the source data (e.g. in line with cells
B2:B6), Excel assumed we want to return the value from the same row.
• If the formula was not in line with the source data (e.g. if we entered the formula
in cell D7), Excel got confused and returned the #VALUE! error.
Look at Figure 1, the formula above has been entered into cells D2, D4 and D6.
The value of 1 is shown in cell D2. Excel assumes we want the value from cell B2 because
it is in the same row. By entering exactly the same formula in cell D4, Excel assumes we
want the inline cell, so returns 3 from cell B4.
Since cell D7 is not in the same row or column with any of the source data, Excel doesn’t
know what we want, and returns the #VALUE! error. This assumption has got a technical
name – ‘implicit intersection’. But due to the new changes in Excel, we don’t really need
to worry about implicit intersection anymore.
4
Figure 1: The old method of Excel calculating arrays.
It’s troubling to think that Excel could calculate different results from the same formula
(hopefully this is a thing of the past). To override implicit intersection, Excel allowed us to
press Ctrl+Shift+Enter when we entered formulas. These were known as array formulas.
When use array formulas, things suddenly became very difficult to understand, even for
advanced Excel users. But with the introduction of dynamic arrays, we don’t need to worry
about Ctrl+Shift+Enter array formulas anymore either.
If we have a newer, dynamic array enabled version of Excel, there is only one result for our
example formula.
Look at Figure 2. Excel returns all 5 cells in a single calculation. Cell D2 contains the
formula, but the result is shown in cells D2, D3, D4, D5 and D6. One formula displays 5
results… amazing!
The basic rule of one formula one cell has gone. The terminology to describe a formula
filling multiple cells is “spilling”, and the range of cells filled by that formula is called the
“spill range”.
5
Figure 2: The new method of Excel calculating arrays.
6
Figure 3 is a simple scenario in which we have data in cells B2:C9. This data displays a
name and a score. Let’s assume the goal is to calculate the total score for each person.
Before dynamic arrays, to calculate the total score for each individual, we could use the
SUMIFS function (Figure 4).
Cells F2:F5 each contain a formula. For example, F2 contains the following:
=SUMIFS($C$2:$C$9,$B$2:$B$9,E2)
To calculate the result for each person, this formula would be copied down into the 3 rows
below. In each formula, the last argument would change to reference cells E3, E4 and E5,
respectively.
But wait, the new Excel calculation engine can return multiple results from a single
formula. Much like how our basic formula at the beginning of the chapter pushed results
into other cells, SUMIFS works the same.
7
Figure 5: SUMIFS with dynamic arrays.
As can be seen by Figure 5, rather than having 4 formulas, one for each cell; we can have
one formula which returns results into 4 cells. The formula in cell F2 demonstrates this:
=SUMIFS(C2:C9,B2:B9,E2:E5)
The last argument in the SUMIFS function is the value to be matched. Rather than one
value, we have provided an array of values to match in cells E2:E5. Excel has performed
all 4 calculations and returned the results into cells F2:F5.
When a calculation in a single cell creates multiple results, this is known as spilling.
So, which formulas spill, and which don’t? Good question. It depends on the arguments
that the formula expects.
Basic aggregation functions, such as SUM, AVERAGE, MIN, MAX, etc., will not spill by
themselves, which makes sense as they accept a range of values and only ever return a
single value.
Generally, any function containing an argument in which a single value is expected is likely
to spill. That single value has a technical term, it’s known as a scalar.
Think about VLOOKUP as an example. The first argument in VLOOKUP is the value to
lookup, it’s generally a single value (i.e., it is a scalar). If we provide two or more values in
that argument, the formula will spill, and calculate the result for each item included within
the lookup value.
Look at Figure 6. The VLOOKUP in cell F7 is looking up cells E7 and E8 (the values Dave and
Jack), so it calculates for each cell and returns the values in F7 and F8.
8
Figure 6: VLOOKUP spills values into other cells.
Generally, the rule is that If we’re working with standard formulas, any time we use
multiple scalars, it will spill.
Spilling
By clicking a formula or any cell in the spill, a blue box is displayed to outline all the cells
within the spill range. Everything within the blue box is calculated by the top-left cell of
that box.
By selecting any cell within the spill range, the formula bar displays the formula driving
that result. If it is the top-left cell, we can edit the formula. However, if we select a cell,
other than the top-left, the formula is greyed out and can’t be edited.
Look at Figure 8; we have selected the second cell in the spill range. The formula is greyed
out; we can’t edit the formula in that cell.
9
Figure 8: Formula is greyed-out because selected cell is not the top left cell in the spill range.
What happens if there is data already in the spill range? Will it overwrite the existing data?
Thankfully, nothing too dramatic happens. Instead, the top-left cell returns a #SPILL! error
(Figure 9).
Figure 9: #SPILL! error is returned if another cell in the spill range is used.
By clicking on the #SPILL! error, Excel displays the spill range, and we can see what is
causing the problem. As can be seen by Figure 10, the value of Something is within the
spill range of the formula.
Figure 10: Clicking on #SPILL! error shows where the formula is trying to spill into.
10
Then it’s your choice to move/delete the value which is blocking the formula.
• The spill range is so large that Excel has run out of memory
# references
As formulas can spill results into other cells, we need a way to reference all the cells in the
spill range. Thankfully, Microsoft has already thought of this and created a new
referencing methodology using the # symbol.
If the top-left cell in the spill range is cell F2, we could reference the entire spill range by
using F2#.
Figure 11 revisits our earlier example, but with an AVERAGE function added in cell F7.
=AVERAGE(F2#)
11
By using F2#, we are referencing all the cells in the spill range (cells F2, F3, F4 and F5). One
significant advantage is that if the spill range changes size, the AVERAGE will automatically
expand to include the increased range.
Constant arrays
Constant arrays have always existed in Excel; however, given the introduction of dynamic
arrays, the use of constant arrays is likely to increase. They sound more complicated than
they are. So, let’s just spend a few minutes understanding how they work.
The easiest way to understand this is with an example; let’s use VLOOKUP (See Figure 12)
=VLOOKUP(F3,B3:D6,{2,3},0)
This formula includes a constant array of {2,3}. Excel is using values 2 and 3, and returning
calculations for both into cells G3 and H3. The old Excel would have required two formulas
to achieve this, but with dynamic arrays we can use one. The two numbers in curly
brackets are known as a constant array.
With constant arrays, just be aware that columns are separated by commas and rows are
separated by semi-colons. If we wanted to spill in rows instead of columns, we would use
a semi-colon between the values (e.g., {2;3}). See Figure 13.
=VLOOKUP(F3,B3:D6,{2;3},0)
12
Figure 13: Constant arrays in rows use semi-colons.
The @ symbol
If we want to operate in the old way, we use the @ symbol. Let’s head back to our original
example:
=B2:B6
If we wanted to use the old implicit intersection method which only returns the values
which are in the same row, we can add the @ symbol as follows:
=@B2:B6
It’s unlikely that we would ever want to revert to the old way of calculating formulas. But
initially, we are likely to see a lot of the @ symbol. To ensure formulas built in previous
versions of Excel continue to calculate the same result, the @ symbol will be added to
some formulas automatically. This means that workbooks created in old versions of Excel
but opened in the new version of Excel should never spill.
NOTE:
It may seem confusing because the @ symbol is already used within the structured
referencing format that we use with Excel tables. But if you think about it, in Excel tables,
the @ symbol is used to reference items in the same row. Therefore, structured
references already have implicit intersection built in.
13
What next?
I’m sure you’ve got 100 questions spinning around your mind about dynamic arrays. There
is a lot of new terminology and ways of working here. While the changes may seem
confusing initially, you will soon see that this brings new powers to Excel users, which make
Excel easier to use. In this book, we will explore this new power so that you will be able
to use dynamic arrays on a daily basis.
14
The UNIQUE function
I can’t even begin to count the number of times I have created a unique list in Excel. I have
performed it manually using the remove duplicates from the ribbon, with PivotTables and
using complex formulas, but that is now a thing of the past.
The UNIQUE function in Excel can be used to create distinct and unique lists. If new items
are added or removed from the input cells, the result of the function will expand or retract
automatically.
• [occurs_once]: If you want a list that includes only the items that appear once,
then use TRUE. If you want a list that contains one instance of each item (i.e., a
distinct list), then use FALSE. This is an optional argument and if excluded, will
default to FALSE. The impact of this is demonstrated in Example 1.
15
Example 1 – The difference between unique and distinct
The last argument of the UNIQUE function determines if it returns a distinct or unique list.
Distinct list
=UNIQUE(B3:B10)
As the third argument has not been used, occurs_once has defaulted to FALSE and
therefore shows a list of distinct results. Sally, Jack, Billy, Ryan, Chau and David all appear
in cells B3:B10; therefore, we get a list of all those names.
=UNIQUE(B3:B10,,TRUE)
The third argument is TRUE, therefore UNIQUE will return the results which appear only
once in the array. Sally, Billy, Ryan, and David all appear only once within cells B3:B10.
However, Jack and Chau appear more than once and are therefore excluded from the
result.
16
Figure 15: Unique lists contain only the items which appear once.
Method 1
The first method returns the unique list and retains the same number of columns as
included in the array argument.
17
The formula in cell G3 of Figure 16 is:
=UNIQUE(B3:C10)
This includes the First and Last name columns in the array and returns both in the result.
One instance of Chau Yang has been excluded as it appears twice in the source list.
Method 2
The second method uses functionality from the new calculation engine to join columns
before applying the UNIQUE function.
=UNIQUE(B3:B10&" "&C3:C10)
Again, one instance of Chau Yang has been removed to provide a unique list within a single
column.
Method 3
Sometimes we want a unique list with two columns that are not next to each other. In this
circumstance, we can use the CHOOSE function to reorder the columns.
18
The formula in cell G3 of Figure 18 is:
=UNIQUE(CHOOSE({1,2},E3:E10,B3:B10))
By using CHOOSE, we have defined the first array as E3:E10 and the second array as
B3:B10. These are the ranges that have been returned within the spill range.
=UNIQUE(D2:I2,TRUE)
The second argument of TRUE is used to tell the function that the data is in a horizontal
format.
19
Figure 19: The UNIQUE function works across columns.
If we had a vertical or horizontal list that we wanted to flip, we could use the TRANSPOSE
function.
=TRANSPOSE(UNIQUE(D2:I2,TRUE))
TRANSPOSE is used to change our horizontal UNIQUE list, so the output is vertical.
20
Example 4 – Combining UNIQUE with SORT in a data
validation list
Example 4 demonstrates how to combine the UNIQUE and SORT functions together (Figure
21).
=SORT(UNIQUE(C3:C10&", "&B3:B10))
The formula returns an alphabetically sorted unique list based on the last name and first
name combined.
Often the purpose of a unique sorted list is for use within a data validation drop-down list.
To do this, we can use the # symbol after the cell reference to refer to the entire spill range.
In Figure 22, the dynamic array formula is contained in cell G3. Therefore =G3# has been
used as the source for a data validation list. When the spill range increases or decreases
in size, so does the drop-down list. It’s like magic!
21
Figure 22: Use # referencing to add a UNIQUEs spill range to a data validation list.
=UNIQUE(E3:E10)
=TRANSPOSE(UNIQUE(D3:D10))
=COUNTIFS(E3:E10,G3#,D3:D10,H2#)
The COUNTIFS function includes the # references, so it automatically spills in the same way
as the cells it is dependent upon.
22
With these 3 simple formulas we have created a complete report – amazing!
23
The SORT function
The SORT function is one of the most useful new dynamic array functions.
The SORT function sorts an array in ascending or descending order, by row or column, and
using one or more columns in the dataset as the sort order.
• [sort_index]: The nth column or row to apply the sort to. For example, to sort by
the 2nd column, the sort index would be 2. It is possible to sort by multiple
columns, which is covered in Example 5. If this argument is excluded, it will
default to sorting by the first column.
• [by_col]: TRUE = sort by columns, FALSE = sort by rows (if excluded the argument
will default to FALSE).
24
Figure 24: SORT function returns the entire array provided.
=SORT(B3:E10)
This single formula returns eight rows and four columns of data. As the second, third, and
fourth arguments have been excluded, the default has been applied for each of them,
sorting by the first column, in ascending order with data organized in rows.
=SORT(B3:E10,2,-1)
The second argument of the SORT function is the sort_index. The formula above is sorting
by the 2nd column of the array.
The third argument is the sort_order. The -1 in this formula sorts the data in descending
order.
25
Figure 25: SORT applied to the second column in descending order.
=INDEX(SORT(B3:E10,4,-1),SEQUENCE(5),{1,4})
We are using two additional functions in this example, SEQUENCE (also a new dynamic
array function) and INDEX (which is not new, it has been around forever).
The SORT is applied to the 4th column in descending order, we have seen similar examples
to this already.
In the past, this would have needed a lot of calculations, but now it’s possible with a single
formula – Amazing!
26
Figure 26: Setting the SORT function to return a limited number of rows and columns.
=SORT(FILTER(B3:E10,C3:C10>=100))
The FILTER function returns only the rows where the values in cells C3:C10 are 100 or
higher. The SORT is then applied to the result of the FILTER, to provide those filtered rows
in alphabetical order.
27
Example 5 – SORT on multiple columns
SORT can be applied to multiple columns at the same time.
=SORT(B3:E10,{2,1},{1,-1})
• The first, {2,1} is the sort_order, which in this example is sorting by column 2 then
by column 1.
• The second constant array is {1,-1}, which determines how each column sorts.
The first sort (applied to column 2) is in ascending order, and the second sort
(applied on column 1) is in descending order.
Figure 28: SORT applied to two columns with different sort orders.
28
The SORTBY function
The SORTBY function is similar to SORT in its objectives, but with one significant difference.
While SORT requires an index number, SORTBY can be sorted based on any cells (even
those outside of the data set provided).
• [by_array2]: The range of cells or array of values to apply the second sort by. This
argument is entirely optional; you can exclude this if you only need one sort
column.
• [sort_order2]: the sort order to apply to the by_array2 (if used): 1 = ascending, -
1 = descending.
If there is a third, fourth or nth sort required, these can be added just like by_array2 and
sort_order2.
Only the first two arguments are necessary, which are the data and what to sort by.
29
Example 1 – The sort column does not need to be in the
array
Look at Figure 29. In this example, the Employees, Units Sold and Average Price columns
are returned based on the descending order of the values in the Total Value column.
=SORTBY(B3:D10,E3:E10,-1)
Cells B3:D10 are sorted by the values in cells E3:E10 in descending order (as the third
argument in the function is -1). The Total Value column (cells E3:E10) is not within the
result, but it is still possible to use it as the sort column.
Figure 29: SORTBY applies a sort from outside of the array argument.
30
Figure 30: SORTBY uses column names when linked to an Excel table.
=SORTBY(salesTable[[Employee]:[Average Price]],
salesTable[Total Value],-1)
The second argument in the SORTBY function determines the values to sort by. When
linked to an Excel table, this column has a meaningful name. In this example, we are
sorting by the salesTable[Total Value] column. The ability to use descriptive column names
is an advantage over the SORT function, which sorts based on an index column.
=SORTBY(B3:D10,C3:C10,-1,B3:B10,1)
Cells B3:D10 are sorted first by cells C3:C10 (the Units Sold) in descending order, then by
cells B3:B10 (the Employee name) in ascending order.
31
Figure 31: Using SORTBY with multiple columns.
=SORTBY(CHOOSE({1,2},E3:E10,B3:B10),C3:C10,-1,B3:B10,1)
The CHOOSE function is using E3:E10 as the first range, and B3:B10 as the second range.
The {1,2} is telling the CHOOSE function which position each range should be in. If you
were to use {2,1}, the ranges would be returned in the reverse order.
32
Figure 32: Use the CHOOSE function to return the columns in any order.
Using this method, we can return columns in any order; we are not restricted by the layout
of the source data.
33
The formula in cell G3 is:
=FILTER(SORTBY(B3:D10,C3:C10,1),C3:C10>5)
This formula is intended to sort based on cells C3:C10, then filter to only return the rows
where the values in C3:C10 are greater than 5.
But did you notice in Figure 33 that it doesn’t return the correct values? This occurs
because the first argument of the FILTER function uses SORTBY to sort, but the second
argument is still working on the unsorted data. When nesting these formulas, we need to
apply the sort to each argument.
=FILTER(SORTBY(B3:D10,C3:C10,1),SORTBY(C3:C10,C3:C10,1)>5)
Now both arguments of the FILTER function are based on arrays sorted by C3:C10.
34
Example 6 – Restrict the values returned by SORTBY
Finally, what if you only want to return a single sort position? For example, what if we
wanted only the 3rd item from the sorted list? (Figure 35).
=INDEX(SORTBY(B3:D10,C3:C10,1),3,0)
SORTBY is nested within the INDEX function. It is the INDEX function that is returning the
3rd item in the list.
35
The FILTER function
Filtering is a common everyday action for most Excel users. Whether using AutoFilter or
an Excel table, it is a convenient way to view a subset of data quickly. Until the FILTER
function came to Excel, there was no easy way to achieve this with formulas.
FILTER operates like a lookup function, such as VLOOKUP or INDEX/MATCH, but rather
than returning the first result, it returns all the matching results.
• include: An array of TRUE/FALSE results, where the TRUE values will be retained
in the filter.
=FILTER(B3:D10,C3:C10>100)
This single formula returns the data where the values in C3:C10 are higher than 100.
36
Figure 36: FILTER returns an array of values into a spill range.
=FILTER(B3:D10,C3:C10>200)
As no rows meet the criteria of Invoice Value being higher than 200, the FILTER cannot
return a value, so the #CALC! error is displayed.
37
Thankfully, Microsoft has given us the if_empty argument, which displays a message if
there are no rows returned.
Figure 38: Avoiding the #CALC! error by using the if_empty argument.
=FILTER(B3:D10,C3:C10>200,"No Results")
Because the function uses the if_empty argument, “No Results” displays in cell F3, instead
of the #CALC! error.
If we want to display a result in each column, we could include a constant array within the
if_empty argument. The following would show “n/a” in the Invoice Value and Days Due
columns.
=FILTER(B3:D10,C3:C10>200,{"No Results","n/a","n/a"})
38
Figure 39: Displaying an array of results when using if_empty.
Figure 40: Applying filter with multiple criteria using AND logic.
=FILTER(B3:D10,(C3:C10>50)*(D3:D10>30))
For anybody who has used the SUMPRODUCT function, this method of applying multiple
conditions will be familiar.
39
Multiplication with * creates AND logic (i.e., all the criteria must be TRUE). Figure 40 shows
where the Invoice Value (cells C3:C10) is greater than 50 and the Days Due (cells D3:D10)
is greater than 30. By multiplying the results, it is only returns values which are both true.
Addition creates OR logic (i.e., any individual condition can be TRUE) (Figure 41).
=FILTER(B3:D10,(C3:C10>50)+(D3:D10>30))
The example above shows where the Invoice Value (cells C3:C10) is greater than 50 or the
Days Due (cells D3:D10) is greater than 30.
Using + between the two arrays returns results where either condition is true.
=SORT(FILTER(B3:D10,D3:D10<=30))
First, the FILTER function returns the cells based on the Days Due being less than or equal
to 30. The SORT function then puts the Customer into ascending alphabetical order.
40
Figure 42: Using FILTER and SORT together.
41
The SEQUENCE function
The SEQUENCE function creates a list of numbers in a one or two dimensional array.
Please note, Excel creates sequences across columns, before moving down to the next
row. Example 3 covers how to flip that around.
=SEQUENCE(8)
42
Figure 43: SEQUENCE function using only the required arguments.
In this function, only the rows argument is provided; all the optional arguments have been
excluded. Therefore, all the defaults are applied. SEQUENCE has created a list of 8 rows,
1 column, starting at 1 and incrementing by 1 for each cell.
=SEQUENCE(8,3,5,10)
43
This formula creates an array which is 8 rows, 3 columns, starting at 5 and incrementing
by 10. As previously noted, the function increments across the columns before moving to
the next row.
Note: As I am based in the UK, the screenshots show the UK date format (dd/mm/yyyy)
=DATE(2020,SEQUENCE(13),1)
This formula creates a sequence of monthly dates starting on January 1st, 2020. The
SEQUENCE function has been applied to the month argument; therefore, it adds one to
the month for each result. We have requested 13 months in the SEQUENCE, but Excel is
intelligent enough to know that 01/13/2020 doesn’t exist as a UK date format. Instead,
44
the DATE function will cause the result to roll-over into the next year and create
01/01/2021.
=TRANSPOSE(SEQUENCE(8,3,5,10))
This is the same SEQUENCE function as we used in method 2 of Example 1, but it has been
wrapped in TRANSPOSE. Now the numbers increment down each row before going to the
next column.
This can be a little confusing as the rows argument is displaying 8 columns and the column
argument is displaying 3 rows. When using this technique, we need to remember that
TRANSPOSE is applied as the last action, so the rows and columns are switched.
45
=SORT(INDEX(B3:D12,SEQUENCE(COUNTA(B3:B12)/2,1,1,2),SEQUENCE
(1,3)),2)
• SORT(….,2) – finally the result is sorted by the 2nd column, which is the last name
=SORTBY(SEQUENCE(75),RANDARRAY(75))
SEQUENCE creates an array from 1 to 75. This array is sorted by a list of 75 random
numbers created by RANDARRAY.
47
The RANDARRAY function
The RANDARRAY function generates a list of random numbers.
• [integer]: TRUE = return whole numbers only, FALSE = return decimal values to
15 decimal places.
As all the arguments are optional, we can use RANDARRAY(), without any arguments, to
create a single random number.
One attribute to be aware of is that RANDARRAY is a volatile function. This means that it
recalculates each time any cell changes. If you’ve used the RAND or RANDBETWEEN
functions before, this functionality will not be new to you.
48
Example 1 – Basic usage
In this section we see two basic examples of using the RANDARRAY function.
Single dimension
=RANDARRAY(4)
The formula creates an array of random numbers which is four rows and one column.
Multiple dimensions
=RANDARRAY(4,3)
This formula above creates an array of random numbers which is four rows and three
columns.
49
Figure 50: Random numbers generated in rows and columns.
=SORTBY(B3:B10,RANDARRAY(COUNTA(B3:B10)))
Figure 51: Putting values in a random order, using RANDARRAY with SORTBY.
50
Example 3 – Using all the arguments of RANDARRAY
In Figure 52, we can see all the arguments of the RANDARRAY functions.
=RANDARRAY(4,3,50,150,TRUE)
The formula creates an array of results which is 4 rows, 3 columns, a minimum of 50, a
maximum of 150, with only whole numbers.
51
The formula in cell B2 is:
=RANDARRAY()
The default arguments applied by the RANDARRAY function return a single random
number which is the same calculation as the RAND function.
The RANDBETWEEN function returns an integer between an upper and lower limit.
=RANDARRAY(,,10,20,TRUE)
This demonstrates that RANDARRAY has the ability to select a minimum, maximum and
return integers. Therefore, RANDARRY performs the same calculation as RANDBETWEEN.
52
Using dynamic arrays with other
features
So far, in this book, we've looked at how dynamic arrays work and the new functions that
are available to us. Most of our examples have focused on calculations that occur on the
worksheet. But we've not yet considered how dynamic arrays work with other Excel
features, such as charts, data validation, conditional formatting, etc. So, we're going to
cover that in this chapter.
We won't look at every Excel feature, but focus on the most common that we are likely to
use dynamic arrays for.
Example file for this chapter: Dynamic arrays with other Excel features.xlsx
Overview
When thinking about dynamic arrays, it's useful to make a clear distinction between ranges
and arrays. We often use these terms interchangeably, but they are not the same, and
Excel certainly applies them differently. If we have a clear grasp of this, it will be easier to
understand how dynamic arrays behave the way they do.
Excel converts data between arrays and ranges without the user being aware. Let's think
about the journey our data goes through by using the SORT function as an example.
=SORT(B3:C5)
53
Figure 55: Source data and final result of SORT function.
4. Once the result exists on the worksheet, it becomes a range which we can refer
to as E3#
Even though # references are on the face of the worksheet, when called, Excel still needs
to recalculate to understand the size of the spill range.
With these principles in mind, it means that generally speaking, the following statements
hold true:
• Features which can handle arrays and perform calculations can contain dynamic
array formulas
• Features which can handle calculations, but not arrays, can use the # referencing
system
• Features that cannot perform calculations must use other methods to handle this
for them.
54
Excel tables
Dynamic arrays have a love/hate relationship with Excel tables.
If we use an Excel table as the source for a dynamic array formula, everything works
brilliantly. When new data is added to the table, the formula updates automatically to
include the new data. That's the love part.
However, try to do the inverse and put a dynamic array formula inside an Excel table, and
we get the #SPILL! error. That is the hate part. Arrays are about values, and Excel tables
hold values, so why is there a problem?
Figure 56: (1) Using tables as source for dynamic array, (2) using dynamic arrays as source for a table.
In Figure 56:
=SORT(Table1)
This uses the full range of values from Table1 and outputs the result on the face of the
worksheet.
=G3#
This cell reference is the spill range starting in cell G3, but it always results in a #SPILL!
error when used in an Excel table.
55
1. When using a dynamic array function with an Excel table as its source, it is
converting the table into an array and then returning that result somewhere else
on the worksheet. Therefore, the source and output do not cross over.
2. When using a dynamic array inside an Excel table, it causes issues. Both items are
containers for auto-expanding data. If you try to put something which auto
expands into something else that also auto expands, what should happen? Which
should expand first? What if one grows outside the bounds of the other? There
are just too many questions without answers. Therefore, the Excel team have
decided that a #SPILL! should occur.
Name manager
A reader of my blog once commented to me that they thought named ranges were
misnamed, that they should be called named formulas. I have come to agree, as the name
manager can hold a host of things, such as constants, arrays, Excel 4 macros, and formulas.
In this section, we'll look at just two further items (1) dynamic array formulas and (2) spill
ranges.
Arrays
As the name manager can hold arrays and formulas, it is more than happy to hold dynamic
array functions directly too. Figure 57 shows a named range containing the SORT function.
Figure 57: Using a dynamic array function inside the Name Manager.
We can output the named range shown in Figure 57 on the face of the worksheet. In
Figure 58 we have used the named range in cell G3. A spill range has been created down
to J10.
56
Figure 58: Named range containing dynamic array creates spill area.
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 worksheet name and $ symbols
are used to create an absolute reference (Figure 59).
The named range created in Figure 59 has been used in the Figure 60. Cell L3 outputs the
named range and it’s spill range.
57
Figure 60: Using a # reference named range on a worksheet.
As we go through the remainder of this chapter, 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, Excel needs to perform
some background processing to work out how big the spill range is. As the name manager
can perform calculations, it can trigger this processing. Features that don't contain any
calculation ability can use named ranges as their source to handle this for them.
Charts
Charts can be a little picky about dynamic ranges already. The charting engine does not
try to calculate any ranges, but just wants to use a range it has been given. This hasn't
changed with the introduction of dynamic arrays. Even before dynamic arrays, if we
wanted to use formulas such as INDEX or OFFSET to create dynamic ranges, we needed to
place them in a named range to handle the calculation for the chart.
Figure 61 demonstrates that using a spill range directly in a chart source will result in an
error.
58
Figure 61: Using # reference directly in a chart creates an error.
Instead, we need to use a named range containing a spill reference (Figure 62).
Figure 62: Charts can use data from a dynamic array if a name range is used to pre-calculate the
range.
It should be noted that the output of the named ranges must be of the right type of data
for the chart element. By which, I mean that named ranges used for chart values must
contain numbers and named ranges used for axis labels must contain text values.
In this example, we have used a named range containing a spill range; a named range
containing a dynamic array formula will also work.
Linked pictures
Linked pictures are similar to charts, in that they don't try to calculate anything; they just
want to display the range they are given. Therefore, using a # reference directly inside a
linked picture won't work, we'll turn to the name manager once again.
59
Figure 63: Linked picture using dynamic array inside a named range.
If using a linked picture in this way, when the results change, so does the picture; it grows
or shrinks as the spill range grows or shrinks.
For the purposes of demonstration of another technique, I've pulled a bit of Excel trickery
in the named range used in the linked picture in Figure 63. I have joined two spill ranges
together into a single named range (as shown in Figure 64).
Figure 64: Two # spill areas combined into a single named range.
These are two separate spill ranges, which have been combined as a single range using a
colon in between. This creates a range that goes from G3 down to the end of the spill
range for H3#. This technique is covered in more detail in the next chapter.
60
PivotTables
Initially, PivotTables appear to be happy using the # reference as a source, as shown by the
Figure 65.
But don't be fooled, as soon as we click OK, Excel converts the # reference to a standard
static range. Figure 66 shows that 'Pivot Table'!$G$3# has been converted to a static range
'Pivot Table'!$G$2:$J$10
Figure 66: # reference is converted to a standard range when used as PivotTable source directly.
61
Instead, we must turn to named ranges once again.
PivotTables make an assumption about our data, which is that the first row is a header
row. Which gives us an issue as # references would generally exclude the header. But we
can pull some more Excel trickery.
To include the header row, we can use the following formula in a named range; it creates
a dynamic range from G2 to the bottom of the G3# spill range.
When using this advanced technique with dynamic arrays, the first range must be a spill
range. Then a colon is used (this is known as the range operator). Finally, the second
range after the colon can be either a standard or spill range.
Learn more using the range operator with dynamic arrays in the next chapter.
62
Figure 68: Dynamic array formula containing header row used within a named range.
This now correctly includes the header row so that we can use it for the PivotTable (Figure
69).
Figure 69: Final result of using a dynamic array as source for a PivotTable.
63
Conditional formatting
Conditional formatting is not compatible with dynamic arrays. If we use # references
inside a conditional format, Excel will convert it to a static range, and it will no longer be
dynamic.
Figure 70 demonstrates that when using a spill range ($B$3#), clicking Apply will
automatically convert the range to a static range ($B$3:$E$10).
To use conditional formatting with dynamic arrays, we have to use a work around. We
have to select a static range that is bigger than our likely output and apply the conditional
format to all those cells. That will give the appearance of being dynamic, but may need
64
maintenance from time to time when the spill range expands further than the conditional
formatted cells.
Data validation
Data validation lists perform calculations; therefore, they work exceptionally well with the
# referencing system by themselves. But they don't use arrays, so can't hold a dynamic
array formula.
Figure 71 shows a data validation list containing the spill range starting in cell G3.
The spill range must 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 containing a formula or spill range, that will work too.
65
Advanced formula techniques
In the final chapter, we’ll look at a few advanced formula techniques. We won’t be
covering the individual functions in detail but considering how we can combine them to
solve some tricky problems. Many of these techniques have been covered briefly as
examples in previous chapters, but now we’ll dig a bit deeper.
These are all separate topics, but when the techniques are combined, we can achieve some
amazing things.
The goal of this example is to calculate the total invoice value for each customer using the
UNIQUE and SUMIFS functions.
66
Figure 72: Source data for single vs cascading methodology example.
Cascading formulas
This first option (Figure 73) uses two separate formulas in cells E3 and F3.
Figure 73: Cascading methodology uses separate formulas for each array.
=UNIQUE(B3:B10)
This formula creates a distinct list of customers, and outputs the result into the spill range
starting at cell E3. We’ve seen UNIQUE do this before in previous chapters.
67
The formula in cell F3 is:
=SUMIFS(C3:C10,B3:B10,E3#)
This is the standard SUMIFS function using the spill range of the UNIQUE function (from
cell E3) in the last argument.
The point to note here is that there are two separate formulas to achieve the final result.
The second formula relies on the spill range of the first to create its output.
Single formula
In this second scenario (Figure 74), we see that the same result can be created from a
single formula.
=CHOOSE({1,2},UNIQUE(B3:B10),
SUMIFS(C3:C10,B3:B10,UNIQUE(B3:B10)))
We have used CHOOSE to combine the functions into a single array. The first column is
the result of the UNIQUE and the second column is the result of the SUMIFS. As the spill
range of the UNIQUE function does not exist on the face of the worksheet, we can’t use a
# reference; instead, we repeat the UNIQUE function as the last argument of the SUMIFS.
68
Useful supporting functions
When working with dynamic arrays, there are many functions that help us to work with
arrays. Three of the most useful are CHOOSE, INDEX, and SEQUENCE. We’ll consider each
of these in this section.
CHOOSE
Having just looked at an example using CHOOSE, this seems like the obvious place to start.
CHOOSE can be used for array aggregation. In the section above, we took two separate
arrays and combined them into one. As we have seen, this is useful for creating a single
spill range as we purposefully select the values to return.
INDEX
INDEX is a function that can be used to reduce the output of our array function. Look at
Figure 75.
69
The formula in cell G3 is:
=INDEX(SORT(B3:E10,2,-1),{1;3;5;7},{1,4})
The purpose of this example is not to demonstrate SORT, but to show how we can use the
INDEX function with dynamic arrays.
Remember, when working with rows, the constant array is separated by semi-
colons. But when used with columns, constant arrays are separated by commas.
From the examples, INDEX and CHOOSE appear to be performing similar tasks, but if you
think about it, they are actually performing the opposite to each other. In this context,
CHOOSE is aggregating columns into a single array, while INDEX tasks a single array and
selects the data to retain.
SEQUENCE
While SEQUENCE is a dynamic array function in its own right, it is also a great support
function. In the example above, we used the INDEX function with fixed size constant
arrays. To build more dynamic functions, we can turn to the SEQUENCE function.
70
Figure 76: Using SEQUENCE as a support function to determine the values to return.
The result is the same as the previous example, but the formula in cell J3 is:
=INDEX(SORT(B3:E10,2,-1),
SEQUENCE(ROWS(B3:B10)/2,1,1,2),{1,4})
SEQUENCE is creating an array of alternate numbers, replacing the constant array we used
previously. By using SEQUENCE, each of the arguments can be linked to a cell, which
means we could easily make this select every 3rd, 4th, or nth row simply by changing a
single cell value.
Let’s suggest 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
Excel understands this to be a range covering all the cells from A1 to the end of the spill
range starting in A2. It does not matter how big the A2 spill range is; it will automatically
expand or retract, as necessary.
71
To illustrate this concept further, I would like to cover a technique which I picked up from
Jon Acampora (https://www.excelcampus.com)
Figure 77: Example data to demonstrate # reference with the range operator.
In this example, we want to return the items with a count greater than 100, but also
including header and total row.
First, let’s use the FILTER function to return only those items with a count greater than
100. This is nothing tricky; we’ve seen similar examples in the FILTER chapter.
=FILTER(B3:C10,C3:C10>=100)
Next, let’s add column headers above the data. In cell F3, I’ve entered “Item”, and in G3,
I’ve entered “Count”.
72
Figure 78: FILTER function and headers added as first stage of calculation.
Figure 79: Total row has been added as second stage in the calculation.
In cell F2, I’ve entered “Total”, and in cell G2, I’ve added the following formula:
=SUM(INDEX(F4#,SEQUENCE(ROWS(F4#)),2))
Hopefully, you’ll notice that we are applying the techniques from the sections above. In
simple terms, this is returning the SUM of all the rows from the 2nd column of the spill
range starting in F4.
73
The next step is to create an index number, which represents the order we wish to display
the final result (Figure 80).
Figure 80: Index number column added as the third calculation step.
=SEQUENCE(ROWS(F4#))
As the header row should come first, cell E3 has been given a hardcoded index of 0.
Then the formula in cell E2 is calculating the number of rows in the spill range plus 1.
=ROWS(E4#)+1
Now we use SORTBY to create a new spill range with the data displayed in the same order
of the index we created.
=SORTBY(F4#:F2,E4#:E2)
74
Figure 81: Final result shows a total row has been added to a dynamic array.
• F4#:F2 is referring to a range which is starts at cell F2 and finishes at the end of
the spill range starting in cell F4.
The biggest issue with this technique is that the total cannot be automatically formatted.
The header row will not move, so we can format that using the standard tools. But the
total row moves depending on how our data changes (i.e., if the count of Melons increases
to 150, the total row will have to move down to accommodate the additional data row).
So, we must turn to conditional formatting, with a range big enough range to cover any
potential data growth. We covered this in the previous chapter.
As you can see, the final example in this chapter incorporates a lot of our other formula
techniques. It is worth taking the time to understand how it works; if you master this, then
you’ll be able to achieve almost anything with dynamic arrays.
75
The final word
I trust this has given you a solid foundation to using dynamic arrays. Hopefully, you will
agree that with these new functions and techniques that we can achieve things in Excel,
which were previously very complicated, or just impossible.
This is just the start; as more users get hold of the dynamic array functions, you can be
sure that more formula techniques will be discovered. Maybe after reading this book, and
understanding the concepts, it might be you who discovers a new technique.
76