where data is the named range C5:I13. The result is an array with nine sums, one for each row in the range, as seen in column K. Note: in older versions of Excel you can use the MMULT function, as explained below.
With the BYROW function
In Excel 365, the most straightforward way to generate subtotals for each row is with the BYROW function. The purpose of BYROW is to process data in a “by row” fashion. For example, if BYROW is given an array with 10 rows, BYROW will return single array with 10 results. In the example shown, the formula in K5 is: The calculation performed on each row is provided by a custom LAMBDA function, which must return a single result for each row. In this example, the LAMBDA function used in BYROW sums each row like this: The result is an array of sums, one per row, that spill into the range K5:K13. This result is fully dynamic. If data values change, or if the data range expands or contracts, the output from BYROW will update as needed. Although this example deals with totals, the same pattern can be used to calculate other information about rows, including max, min, average, etc. like this:
With the MMULT function
Another way to solve this problem is with the MMULT function, which performs matrix multiplication. MMULT takes two arrays, array1 and array2, and requires that the number of columns in array1 be the same as the number of rows in array2. The resulting matrix will have same number of rows as the first matrix, and the same number of columns as the second matrix. The MMULT formula looks like this: The first array is simply all values in data, the named range C5:I13: To protect against blank cells, which will cause MMULT to throw #VALUE! error, we use a double negative (–) to force any empty cells to zero. Next, we need to create array2. The first array contains 7 columns, so we need the second array to contain 7 rows. We want just a single column of results, so the second array should be 7 rows by 1 column (7 x 1). Also, because we don’t want to change any values, the array should contain only the number 1 (i.e. multiplying by 1 does not change the original value). Array2 is generated with the TRANSPOSE function and the COLUMN function like this: While slightly cryptic, this syntax above is a clever way to accomplish the task. The COLUMN function returns a 1 x 7 array of column numbers: Next, these numbers are raised to the power of zero with exponent operator (^), which creates a 1 x 7 array of 1s: And the TRANSPOSE function flips the array from 1 x 7 to 7 x 1: The result is handed off to the MMULT function as array2. The MMULT function then performs matrix multiplication with the two arrays, and returns a subtotal for each row: returns the array: These values are returned to cell K5, and spill into the range K5:K13.
SEQUENCE alternative
Another way to construct array2 inside MMULT is with the SEQUENCE function like this: This formula works the same way, but array2 is created with the SEQUENCE function directly: Note we use the COLUMNS function to tell SEQUENCE how many rows to create (7).
Dave Bruns
Hi - I’m Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.