# How To Sum A Dynamic Range: Google Sheets (Example)

How do you apply the sum to a certain number of cells according to another input cell?

The ``` SUM ``` function works amazingly well when the range sought for its total value is static, and one way of making a ``` SUM ``` function somewhat dynamic is to make the cells it references change, but what if you need the ``` SUM ``` function to total a range according to an input cell?

Something that would look a little like this in words:

``=SUM(Starting at A1, or A2, add the next 5 cells below, or maybe 10)``

## ``` INDIRECT ``` Function

One way to create a dynamic range is by using the ``` INDIRECT ``` formula. The ``` INDIRECT ``` formula has two parameters, with the first being the reference needed as a string, and the second determining if the type of reference entered is in ``` A1 ``` notation.

For example, if I wanted to sum the values from cell ``` A1 ``` to a dynamic number of rows, then I could use the following formula:

In the above example, there is one ``` INDIRECT ``` formula that produces a reference to ``` "A2" ``` being the second row in the first column. I could have written the formula without using an ``` R1C1 ``` notation, and it would have produced the same result:

The second example is a little more succinct than the previous, but both examples help to show the possibility of how to use the ``` INDIRECT ``` function.

## What If The Entire Range Is Dynamic?

In our examples above the starting range was fixed and static at cell ``` A1 ``` , but what if the starting range also needs to be dynamic?

You can use two ``` INDIRECT ``` formulas in the ``` SUM ``` function if the starting range is also dynamic.

For example, to set the starting cell reference dynamically you could apply the same principles as seen above, just using the concept twice:

As seen from the above example, cells ``` B2 ``` and ``` B3 ``` set the dimension of how to perform the dynamic sum on the range.

This could also be transposed to using ``` R1C1 ``` type references, as seen here:

## What If The Range Is Not Joined?

So far I’ve assumed the range being dynamically totalled is defined easily in one area. Can the ``` INDIRECT ``` formula be used multiple times in the ``` SUM ``` function when ranges are not adjacent to one another?

By changing the colon ``` : ``` to a comma ``` , ``` in the ``` SUM ``` function we can effectively sum multiple ranges using the ``` INDIRECT ``` formula.

Furthermore, conditions could be placed on the ``` INDIRECT ``` formula so that its reference is not used, for example if the dynamic cell is empty then swap for the value of zero:

By wrapping the ``` INDIRECT ``` formula in an ``` IF ``` statement to check if the dynamic value exists can help reduce the ``` #REF! ``` errors that would occur.

## ``` OFFSET ``` Function

Another handy function that can help dynamically sum your required ranges, is to use the offset function.

The ``` OFFSET ``` function requires 3 parameters, and has an additional 2 more. It looks like this:

``=OFFSET(cell_reference, offset_rows, offset_columns, height, width)``

Using the common example applied so far, this would look something like this:

In the example above, starting with a known fixed cell ``` A1 ``` I then define the area needed to sum by using the dynamic value and setting this as the ``` height ``` parameter. As the column remains the same the ``` width ``` is set to ``` 1 ``` , but can be left out (default value is ``` 1 ``` ).

If the starting value is also dynamic then the same principle as used above can be used: incorporating the ``` INDIRECT ``` formula to help declare the starting cell of the range.

The ``` OFFSET ``` function can be neater than using multiple ``` INDIRECT ``` formulas, provided the range can be defined easily in one area.

## Summary

Dynamic ranges in spreadsheets can be defined using two popular functions: ``` INDIRECT ``` and ``` OFFSET ``` . The ``` OFFSET ``` function is a handy function where the area being worked on is defined in an area, whereas ``` INDIRECT ``` should be able to handle most use cases.

Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.