Dynamic sum in excel

Creating a SUM is very easy to do in Excel. But when you want to add only specific cells and not all of them, the formula is not as easy. But it’s possible! Let’s see how to create a dynamic SUM.

Example dynamic SUM Excel

Insert your data into a Table, the easiest solution

If your need is to dynamically update your sum when you add new data, the easiest solution is to insert your data in a Table. Inserting your data into a table is very useful because your formulas refer to a named range and not to the references of cells.

Let’s see how it works with this basic example.

We have a list of orders for each customer.

We create the formula SUM to calculate the sum of the orders. But here, instead of using the references of the cells, we use the name of the columns.

The formula is:

=SUM(Table1[Orders])

Now if we add new values to our data, we don’t have to change the formula because the named range of the table has automatically
been increased.

Dynamic SUM with the INDEX function

In some circumstances, you can’t use a Table to create a dynamic SUM. This is the case when you have, for instance, payments received and payments due.

In the following Excel file, we have the estimated monthly amount due to be repaid on an investment. But each month we readjust the document with the actual amount paid.

So we build a dynamic range to indicate that the sum will be calculated up to a specific date.

Integration of the INDEX function in the formula

In its standard use, INDEX returns a value inside a table like the VLOOKUP function. But there is also another use of INDEX.

INDEX returns the reference of a cell, and not the value, when the function is used inside a range of cells.

The difficulty here is to trust your writing because you can’t visualise the result in a cell.

Meaning of the MATCH function

The dynamic sum is linked to a date in the data validation cell (cell H4 in our example)

First we need to find the position of the selected date in the range of dates. That’s exactly the purpose of the MATCH function.

=MATCH(H4,E2:E13,0)

Meaning of the INDEX function

The INDEX function returns a value inside a data range. The second parameter of INDEX is the position of the element we want to return.

The INDEX function is written as follows:

=INDEX(range,position row, position column)

In our example the range is the range of a value (in column F) and the position is the position of the date returned by the MATCH function.

=INDEX(F2:F13,MATCH(H4,E2:E13,0))

INDEX returns a reference

But the INDEX fonction also returns the reference of the result.

So if you integrate the previous formula inside the range of a SUM function, the INDEX will return the reference of the cell and not the contents of the cell.

=SUM(F2:INDEX(F2:F13,MATCH(H4,E2:E13,0)))

In this example, the goal is to create a formula that performs a dynamic two-way sum of all City and Size combinations in the range B5:D17 . The solution shown requires four basic steps:

  1. Create an Excel Table called data
  2. List unique cities with the UNIQUE function
  3. List unique sizes with the UNIQUE function
  4. Generate sums with the SUMIFS function

Create the Excel Table

One of the key features of an Excel Table is its ability to dynamically resize when rows are added or removed. In this case, all we need to do is create a new table named data with the data shown in B5:D17.  You can use the keyboard shortcut Control + T.

Video: How to create an Excel table

The table will now automatically expand or contract as needed.

List unique cities

The next step is to list the unique cities in the «City» column starting in cell F5. For this we use the UNIQUE function. The formula in F5 is:

=UNIQUE(data[City]) // unique city names

The result from UNIQUE is a spill range starting in cell F5 listing all of the unique city names in the City column of the table. This is what makes this solution fully dynamic. The UNIQUE function will continue to return a list of unique cities, even as data in the table changes. 

Video: Intro to the UNIQUE function

List unique sizes

Two perform a two-way sum, we also need a list of unique sizes starting in cell G4. We can do this with a similar formula:

UNIQUE(data[Size]) // unique sizes

However, unlike cities, we need the list of sizes to run horizontally across above the sums. To change the output from vertical to horizontal, we nest the UNIQUE formula in the TRANSPOSE function. The final formula in G4 is:

=TRANSPOSE(UNIQUE(data[Size])) // horizontal array

The UNIQUE function returns a vertical array like this:

{"L";"M";"S"}

And the TRANSPOSE function converts this array into a horizontal array like this:

{"L","M","S"}

Note the comma instead of a semicolon in the second array. The UNIQUE function will continue to return a list of unique sizes, even if data in the table changes and sizes are added or removed.

Video: What is an array?

Generate the sums

We now have what we need to calculate the sums. Because we have both unique cities and unique sizes on the worksheet as spill ranges, we can use the SUMIFS function for this task. The formula in G5 is:

=SUMIFS(data[Qty],data[City],F5#,data[Size],G4#)

The first argument in SUMIFS is sum_range. This is the range that contains numbers to sum. In this example, this is the Qty column in the table:

data[Qty] // sum_range

The other arguments are range/criteria pairs. The first pair targets cities:

data[City],F5# // all cities, unique cities

The second range/criteria pair targets sizes:

data[Size],G4# // all sizes, unique sizes

When data changes

The key advantage to this formula approach is that it responds instantly to changes in the data. If new rows are added that refer to existing cities and sizes, the spill range remains the same size, and SUMIFS simply returns an updated set of sums. If new rows are added that include new cities and/or new sizes, these are captured by the UNIQUE function, which expands the spill ranges in F5 and G4 as needed. Likewise, if rows are deleted from the table, spill ranges are reduced by UNIQUE as needed. In all cases, the spill ranges represent the current list of unique cities and sizes, and the SUMIFS function returns a current set of sums.

Legacy Excel workaround

Dynamic array formulas are new in Excel 365 and Excel 2021. In legacy versions of Excel that don’t support dynamic array formulas, it is still possible to compute the sums in G5:I9 with the SUMIFS function. However, certain references must be carefully locked* so that the formula can be copied across and down:

=SUMIFS(data[[Qty]:[Qty]],data[[City]:[City]],$F5,data[[Size]:[Size]],G$4)

Since the UNIQUE function is not available in older versions of Excel, this formula requires that Cities in F5:F9 and Sizes in G4:I4 be created manually.

* This is a good illustration of a key benefit of dynamic array formulas: because there is just one formula, there is no need to use complicated mixed and absolute references. Dynamic array formulas are therefore easier to create and maintain.

Pivot Table option

A pivot table would also be a good way to solve this problem and would provide additional capabilities. However, one drawback is that pivot tables need to be refreshed to show the latest data. Formulas, on the other hand, update instantly when data changes.

Dynamic Array Training

If you need training for dynamic arrays in Excel, see our course: Dynamic Array Formulas.

If you use Excel 365 say goodbye to legacy array formulas (Ctrl+Shift+Enter). Let’s embrace dynamic array formulas that Spill.

I have three different spill/dynamic array formulas to sum each row in a matrix in Excel 365.

They are;

  1. MMULT – Recommended ✓
  2. SUBTOTAL 109 and OFFSET Combo – Recommended ✓
  3. + Arithmetic Operator – Not Recommended

Among the three Excel formulas, the first two are dynamic which means worth it when you have numbers in several columns to sum in each row.

No doubt, in Excel, the easiest way to calculate the sum of each row in a matrix is using the AutoSum feature. But it has one drawback.

The drawback is it won’t do the calculation in a newly inserted row.

In the below example, I use the AutoSum under the menu Formulas ->Function Library to get the sum in each row.

Total Each Row Using AutoSum

If I insert a row and put some values, the E column won’t have the total in that row.

Spill Formulas to Sum Each Row in a Matrix in Excel 365

We were going to try my first Excel formula to sum each row in Excel 365. It’s the + arithmetic operator-based one.

In this Excel formula, we should specify each column in the range individually. So it’s not a dynamic one.

Non-Dynamic Array Formula to Get-Row Wise Total

In cell E2 key in the below Excel formula.

=IF(
   A2:A100="",
   "",
   B2:B100+C2:C100+D2:D100
)

It will fill the adjoining cells down.

Here I think you may only require the explanation for the logical test in the formula.

It’s like this.

If A2:A100 is blank, return blank, else do the addition.

The logical test will only return the sum in rows that contain values in A2:A100. It’s a tricky way to exclude blank rows in Excel formulas.

Dynamic Array Formulas to Sum Each Row in Excel 365

Here I have two options in Excel. Both are dynamic. So beneficial when you have several columns with numbers and to get the sum in each row.

MMULT for Row-Wise Total

My first dynamic array/spill formula to sum each row in Excel 365 uses the MMULT matrix product function.

=IF(
   A2:A100="",
   "",
   MMULT(
      IF(B2:D100="",0,B2:D100),
      TRANSPOSE(SIGN(COLUMN(B1:D1)))
   )
)

Array Formula to Sum Each Row in Excel 365

This Excel 365 formula is for cell E2, which will spill/fill the results down.

Formula Explanation

Here there are two logical tests in this Excel 365 formula.

Similar to the above non-dynamic formula, I have used the first logical test to exclude blank rows in the sum range in Excel Spreadsheet.

The role of the second logical test (the one within the Excel MMULT) is to fill blank cells in the sum range with 0. Otherwise, MMULT ends up returning the error #VALUE! (wrong data type error).

MMULT takes two arrays to multiply. The above second IF test is array 1 in MMULT.

Array 1 (Matrix 1): IF(B2:D100="",0,B2:D100)

Array 2 is a column filled with numbers 1. The number of rows in this column will be equal to the number of columns in the range B1:D1 (width).

Array 2 (Matrix 2): TRANSPOSE(SIGN(COLUMN(B1:D1)))

The matrix multiplication of the above array 1 and 2 will be the row-wise total in Excel.

Do you still have any doubts about sum each row in Excel using MMULT? This illustration/example may help. Highlighted Array 1 and 2.

Sum Each Row in Excel Using MMULT

SUBTOTAL 109 and OFFSET Combo for Row-Wise Total

Here is my third dynamic array formula to sum each row in Excel 365.

It is also for cell E2, which will spill the results down.

=IF(
   A2:A100="",
   "",
   SUBTOTAL(109,OFFSET(B2,SEQUENCE(ROWS(B2:B100),1,0),,,COLUMNS(B2:D2))
)
)
Formula Explanation

Let me walk you through how the above Subtotal formula sums each row in Excel 365.

You May Like: Differences Between SUBTOTAL and AGGREGATE Functions in Excel.

Using =SUBTOTAL(109,B2:D2) we can sum the first row in the range. With the help of the Offset, we can change B2:D2 in each row.

I mean, in the next row, the formula will automatically return the result for =SUBTOTAL(109,B3:D3) and so on.

Here is how.

Offset Syntax: =OFFSET(reference, rows, cols, [height], [width])

Let’s take a look at every argument in the Offset within the above Subtotal formula.

Reference – B2 (The very first cell in the range)

Rows – SEQUENCE(ROWS(B2:B100),1,0)

It returns the numbers from 0 to 98 vertically. It forces the Offset function to offset 0 rows, 1 row, 2 rows, 3 rows, and so on in each row)

Related: The Sequence in Excel 365 – Dynamic Array Function.

Cols – We don’t want to offset any columns.

Height – I have omitted this argument as we require only 1 row each.

Width – COLUMNS(B2:D2)

It returns the total number of columns in the range.

The above are the three best formulas to sum each row in Excel 365.

Similar Tutorials:

  • Find the Largest Value in Each Row or Column in Excel 365.
  • Find the Smallest Value in Each Row or Column in Excel 365.


Accountants often want to sum data, such as year-to-date costs and rolling budgets, for a period of time. Excel XLOOKUP makes the process simpler.


As accountants, we often want to sum data for a period of time, eg, sales for the last quarter, year-to-date costs, and rolling budgets/forecasts. We generally create these calculations «statically»; ie, the range is specified explicitly.

For example, we might have something like the following example:

xlookup-1

Here, we have created a static formula for the year-to-date sales using the calculation

=SUM(F4:J4)

Next month, we would have to update this to

xlookup-2

=SUM(F4:K4)

We can’t simply sum the range in case other users populate row 4 with forecast estimates, etc. There is an alternative, and I thought I would go up to date and use Excel 365’s XLOOKUP function.

As a reminder, XLOOKUP has the following syntax:

XLOOKUP(lookup_value, lookup_vector, results_array, [if_not_found], [match_mode], [search_mode])

This function seeks out a lookup_value in the lookup_vector and returns the corresponding value in the results_array. It may seem complex, but most of the time you will require only the first three arguments:

  • lookup_value: This is required and defines what value you want to look up.
  • lookup_vector: This reference is required and is the row or column of data you are referencing to look up lookup_value.
  • results_array: This is where the corresponding item is you wish to return and is also required (even if it is the same as lookup_vector). This does not have to be a vector (ie, one row or one column of cells). It may be an array (with at least two rows and at least two columns of cells). The only stipulation is that the number of rows/columns must equal the number of rows/columns in the lookup_vector.

For the record, the remaining arguments are:

  • if_not_found: This optional argument allows you to replace the usual return of #N/A with something more informative like an alternative formula, text, or a value.
  • match_mode: This argument is optional. There are four choices:
    • 0: Exact match (default).
    • -1: Exact match or else the largest value less than or equal to lookup_value.
    • 1: Exact match or else the smallest value greater than or equal to lookup_value.
    • 2: Wildcard match. You should use the special character ? to match any character and * to match any run of characters.

What’s impressive, though, is that for certain selections of the final argument (search_mode), you don’t need to put your data in alphanumerical order. As far as I am aware, this is a first for Excel.

  • search_mode: This argument is also optional. There are again four choices:
    • 1: Search first to last (default).
    • -1: Search last to first.
    • 2: What is known as a binary search, first to last (requires lookup_vector to be sorted). Just so you know, a binary search is a search algorithm that finds the position of a target value within a sorted array. A binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value and repeating this until the target value is found.
    • -2: Another binary search, this time last to first (and again, this requires lookup_vector to be sorted).

Let’s have a look at XLOOKUP versus everyone’s favourite function (except me), VLOOKUP:

xlookup-3

You can clearly see the XLOOKUP function is shorter:

=XLOOKUP(H52,F41:F47,G41:G47)

Only the first three arguments are needed, whereas VLOOKUP requires both a fourth argument and, for full flexibility, the COLUMNS function as well. XLOOKUP will automatically update if rows/columns are inserted or deleted. It’s just simpler.

We can use this to specify the start and end of our sum range as follows. Consider the following example:

xlookup-4

The formula is «simply»

=SUM(XLOOKUP(G18,H12:S12,H13:S13):XLOOKUP(G19,H12:S12,H13:S13))

This is just two XLOOKUP functions joined together within a SUM function, specifying the start and end of the range. Indeed, if the First Date is after the Second Date, it will still work. The SUM function will work in «reverse order», too.

The dates may be varied, and the summation updates both automatically and correctly, as shown below.

xlookup-5

Simple!

Word to the wise

For those who are getting upset at this point because they don’t have access to Excel 365 and/or the XLOOKUP function, do not despair. The old faithful INDEX(MATCH) combo still works. It’s just clunkier:

xlookup-6

=SUM(INDEX(H13:S13,MATCH(G18,H12:S12,0)):INDEX(H13:S13,MATCH(G19,H12:S12,0)))

Lovely! Until next time …

Liam Bastick, FCMA, CGMA, FCA, is director of SumProduct, a global consultancy specialising in Excel training. He is also an Excel MVP (as appointed by Microsoft) and author of Introduction to Financial Modelling. Send ideas for future Excel-related articles to him at liam.bastick@sumproduct.com. To comment on this article or to suggest an idea for another article, contact Jeff Drew at Jeff.Drew@aicpa-cima.com.

43

43 people found this article helpful

Combining the INDIRECT and SUM functions

In Excel, you can sum a number of cells using a variable range with the INDIRECT function. The INDIRECT function automatically updates the range of cells you’ve referenced without manually editing the formula itself. You can use the INDIRECT function with any number of Excel functions, but the most common (and useful) is when you use the SUM function.

The information in the article applies to Excel versions 2019, 2016, 2013, 2010, and Excel for Mac.

How to Sum a Number of Cells Using a Variable Range

You can embed the INDIRECT function as an argument inside of the SUM function to create a variable range of cell references for the SUM function to add. The INDIRECT function does this by referencing the range of cells indirectly, through an intermediate cell reference.

The format of both functions used together looks like this:

=SUM(INDIRECT("D1":D4)

This locks the range to start at D1 and allows D4 to change if you insert or delete any cells in the D column.

You can also use other cells in the spreadsheet to modify the cell references. For example, if you use E1 to reference the first cell of the range and E2 to reference the last cell of the range, the formula looks like this:

=SUM(INDIRECT("D" &E1& ":D" &E2))

By changing the numbers located in cells E1 and E2, you can modify the range in the formula without having to manually edit the formula.

Try the SUM and INDIRECT Functions

Create a spreadsheet to test the SUM and INDIRECT functions yourself. Begin by creating a blank spreadsheet and entering the following data into columns D and E:

Cell Data
D1 - 5
D2 - 10
D3 - 15
D4 - 20
D5 - 25
D6 - 30
E1 - 1
E2 - 4

Next, create the formula in cell F1. Here’s how:

  1. Select cell F1. This is where the result of this example will display.
  2. Select Formulas.
  3. Choose Math & Trig to open the function drop-down list.
  4. Select SUM in the list to open the SUM Function Arguments dialog box.

Nest the INDIRECT Function into the SUM Function

Next, enter the INDIRECT function into the SUM function using this dialog box.

  1. In the Number1 field, enter the following INDIRECT function:
     INDIRECT("D"&E1&":D"&E2) 
  2. Select OK to complete the function and close the dialog box.
  3. The number 50 appears in cell F1. This is the sum of cells D1 to D4.
  4. When you select cell F1, the formula field shows:
    =SUM(INDIRECT("D"&E1&":D"&E2))

When you understand how to format the SUM and nested INDIRECT functions, you’ll be able to type the entire function shown in step 4 directly into the result cell (in this example, cell F1).

Build the Argument for the INDIRECT Function

In the example above, the references to cells E1 and E2 are outside of the quotes. This, combined with the & concatenate symbol, embeds whatever is contained inside of cells E1 and E2 into the formula itself.

Here’s how the elements of the formula work:

  • & is used to join together text data (in this case the letter D) with a cell reference (E1 and E2) that contains a number value.
  • » « must surround text data inside of the cell references (in this example, the letter D).
  • : is a separator between the first cell and the last cell in the SUM range.

In the example above, the start point of the range is defined by the characters: «D»&E1.

The endpoint of the range is defined by the characters: «:D»&E2

The colon needs to be included inside the quotation marks.

The third ampersand in the middle is used to concatenate the two segments into one argument:

"D"&E1 & ":D"&E2

The SUM Function Variable Range

The INDIRECT function nested inside of the SUM function makes it easy to change the start and end of the range totaled by the SUM function, without having to edit the function itself.

Using the concatenation technique in step 3, use the numbers stored in cells E1 and E2 in the spreadsheet to change the range of cells for the SUM function.

Test this in your own spreadsheet by modifying cells E1 and E2 and watch how the SUM function results change.

  1. Select cell E1.
  2. Type the number 3.
  3. Press Enter.
  4. Select cell E2.
  5. Type the number 6.
  6. Press Enter.
  7. The answer in cell F1 changes to 90. This is the sum of the numbers contained in cells D3 to D6.

To see the INDIRECT function in action, insert a new cell into cell D3. This shifts all of the other cells down. The new sum is the total of cells D3 to D7. The INDIRECT function dynamically changes the entire range to include the entire list you initially entered, plus the new cell you inserted as well.

The #REF! Error Value

The #REF! error value appears in cell F1 if the INDIRECT function:

  • Contains an invalid cell reference.
  • Contains an external reference to a different worksheet that doesn’t exist.
  • Refers to a cell range outside of the worksheet’s limits.
  • Refers to a cell that is blank.

Thanks for letting us know!

Get the Latest Tech News Delivered Every Day

Subscribe

Like this post? Please share to your friends:
  • Dynamic charts in excel
  • Dynamic array in excel
  • Dying is not a word
  • Dwg формат в word
  • Dwg в excel программа