Sub topic 2
Formulas that work on whole ranges
Earlier Excel versions treated a range like B2:B6 as a list of cells that a function such as SUM could read. Ordinary operators such as * and + only worked on single values. Dynamic array Excel changes that. A range is an array of values, and operators and most functions apply to every element at once.
This chapter assumes you can enter formulas, use ranges such as A1:A10, and use basic functions such as SUM and IF. You need Excel for Microsoft 365 or Excel 2021 or later. Each formula below returns an array. This chapter focuses on how the values are calculated. The next chapter covers how the results fill neighboring cells.
The sample data
The examples use this small order list in A1:D6.
| Row | A | B | C | D |
|---|---|---|---|---|
| 1 | Item | Qty | Price | Region |
| 2 | Pen | 10 | 1.50 | East |
| 3 | Notebook | 4 | 3.00 | West |
| 4 | Stapler | 2 | 12.00 | East |
| 5 | Folder | 25 | 0.80 | North |
| 6 | Marker | 8 | 2.25 | West |
Element-wise arithmetic
When you apply an operator to two ranges of the same size, Excel pairs the elements by position. The first element of one range meets the first element of the other, the second meets the second, and so on. The result is a new array of the same size.
=B2:B6*C2:C6This returns five values: 15, 12, 24, 20 and 18. Each value is quantity times price for one row. You do not need a helper column or a fill-down. The formula is entered once, and the result is a single array.
You can pass that array straight to another function. SUM accepts any array, not only a range.
=SUM(B2:B6*C2:C6)This returns 89, the total order value. The multiplication produces an intermediate array in memory, and SUM adds it up. Nothing is written to the sheet in between.
=SUMPRODUCT(B2:B6,C2:C6)SUMPRODUCT returns the same 89. It has always handled arrays natively, which is why older workbooks use it so often. In Microsoft 365 the SUM form is usually easier to read, because the multiplication is visible in the formula.
Mixing an array with a single value
A single value, called a scalar, combines with every element of an array. Excel repeats the scalar as many times as needed.
=C2:C6*1.2This adds 20% to every price and returns 1.8, 3.6, 14.4, 0.96 and 2.7. The scalar can also be a cell reference, such as =C2:C6*$F$1 with the markup rate stored in F1. Use an absolute reference so the rate stays fixed if you copy the formula.
Arrays of different shapes
Two arrays do not need identical dimensions. Excel follows three rules:
- If both arrays have the same shape, elements are paired by position.
- If one is a single column and the other a single row, Excel expands both into a grid. Every row item meets every column item.
- If the shapes cannot be matched, the missing positions return
#N/A.
The second rule is useful for building tables. SEQUENCE(3) is a column of 1 to 3, and SEQUENCE(1,4) is a row of 1 to 4.
=SEQUENCE(3)*SEQUENCE(1,4)The result is a 3-row by 4-column multiplication table:
| Row | Column | Column | Column |
|---|---|---|---|
| 1 | 2 | 3 | 4 |
| 2 | 4 | 6 | 8 |
| 3 | 6 | 9 | 12 |
The third rule produces a visible symptom. Suppose you accidentally use ranges of different lengths.
=B2:B6*C2:C4Excel returns five rows, but only the first three can be paired. The last two cells show #N/A. If you see a column of results that ends in #N/A, compare the lengths of the ranges in the formula.
Comparisons return arrays of TRUE and FALSE
Comparison operators work element by element too.
=B2:B6>5This returns TRUE, FALSE, FALSE, TRUE, TRUE. That array is the key to many array techniques, because TRUE and FALSE can be turned into 1 and 0 with arithmetic. The double minus -- is a common way to do this.
=SUM(--(B2:B6>5))This returns 3, the number of rows with a quantity above 5. The comparison builds the TRUE/FALSE array, -- converts it to 1s and 0s, and SUM adds them.
You can also multiply the comparison by other arrays. A FALSE becomes 0 and removes that row from the total.
=SUM((D2:D6="East")*B2:B6*C2:C6)This returns 39, the combined value of the East orders (15 + 24). Rows from other regions are multiplied by 0.
Combining conditions with * and +
To combine several conditions inside an array formula, use arithmetic operators instead of AND and OR.
- Multiplication (
*) means AND. The result is 1 only when both conditions are 1. - Addition (
+) means OR. The result is 1 or more when at least one condition is 1.
=(D2:D6="East")+(D2:D6="West")This returns 1, 1, 1, 0, 1. Only the North row is 0.
Functions that accept arrays
Many functions that normally take a single value will accept an array and return one result per element. This includes text functions, math functions and IF.
=LEN(A2:A6)This returns 3, 8, 7, 6 and 6, the length of each item name.
=IF(B2:B6>5,"Bulk","Single")This evaluates the condition for each row and returns Bulk, Single, Single, Bulk and Bulk. The IF function picks between the two results row by row, so it behaves like a column of separate IF formulas.
The & operator joins text element by element:
=A2:A6&" ("&D2:D6&")"This returns "Pen (East)", "Notebook (West)", "Stapler (East)", "Folder (North)" and "Marker (West)".
Not every function behaves this way. Functions built to summarize data, such as SUM, AVERAGE and MAX, reduce an array to one value. Passing an array to a function that expects a single value usually returns an array. Passing an array to a function that expects an array or range usually returns one value. If you are unsure, enter the formula in an empty area and look at the result.
How the formula behaves in older workbooks
In Excel versions before dynamic arrays, a formula such as =B2:B6*C2:C6 entered in a single cell did not return an array. Excel applied implicit intersection. It picked the one value from each range that sat in the same row as the formula cell, and the result was a single number. If the formula's row was outside rows 2 to 6, you got #VALUE!. To get a full array result in those versions, you had to enter the formula as a legacy array formula with Ctrl+Shift+Enter, and Excel showed braces around it.
Dynamic array Excel evaluates the formula as an array by default. When you open an older workbook, Excel adds an @ in front of ranges that used to be intersected, so the old result stays the same. A later chapter covers the @ operator and legacy array formulas in detail.
Common mistakes
- Mismatched range lengths.
B2:B6*C2:C4gives#N/Ain the last rows. Check that the ranges have the same number of rows. - Using
ANDorORfor row-by-row logic. These return a single value. Use*and+. - Forgetting that TRUE and FALSE are not numbers.
SUM(B2:B6>5)returns 0, becauseSUMdoes not count Boolean values inside an array. Convert them first with--or by multiplying. - Mixing a row and a column by accident. A 5-row column times a 1-row, 4-column array makes a 5 by 4 grid, not an error. If you get a grid you did not expect, check the orientation of each argument.
- Writing results into cells that are not empty. An array formula needs room for its whole result. This is the cause of
#SPILL!, which is covered later in the series.
Exercise
Using the sample data, write one formula that returns the total value (quantity times price) of all orders from the West region only. What number does it return?
Show answer
=SUM((D2:D6="West")B2:B6C2:C6). The comparison returns FALSE, TRUE, FALSE, FALSE, TRUE. Multiplying by the line totals 15, 12, 24, 20 and 18 leaves 0, 12, 0, 0 and 18, and the sum is 30.
Exercise
What does =SUM(B2:B6>5) return, and how would you change it to count the rows where the quantity is above 5?
Show answer
It returns 0, because SUM ignores the TRUE and FALSE values inside the array. Convert them to numbers first: =SUM(--(B2:B6>5)). This returns 3.
Exercise
You enter =B2:B6*C2:C4 and see #N/A in the last two result cells. Why does this happen, and how do you fix it?
Show answer
The two ranges have different lengths (5 rows and 3 rows). Excel pairs the first three elements and returns #N/A for the two positions that have no partner. Change the second range to C2:C6 so both ranges have 5 rows.