Sum every n rows
In this example, the goal is to calculate a weekly total using the data as shown. Notice each week corresponds to 5 rows of data (Monday-Friday) so we will need to sum a different range every 5 rows. To build a range that corresponds to the correct 5 rows for each week, we use the OFFSET function. To sum the values returned by OFFSET, we use the SUM function. The complete formula in cell F4, copied down, is:
The OFFSET function returns a reference to a range constructed with five inputs: (1) a starting point, (2) a row offset, (3) a column offset, (4) a height in rows, (5) a width in columns. In the example shown, we use OFFSET inside of SUM like this:
The starting reference is C5, since this is the first value in the data. Notice we use an absolute reference because we don't want to this reference to change later when we copy the formula down. The next argument, rows, the crux of the problem because this is where we need logic that will figure out the correct starting row for each week.
To calculate the right starting point, we use the ROW function. ROW returns the row number for a given reference or, if no reference is provided, the row number of the cell containing the formula. Because the formula sits cell F4, ROW() will return 4. We use this fact to create the logic we need. We first subtract 4 as an "offset" because we we want to create a zero-based index, for reasons made clear below. Then we multiply the result by 5, this is how this snippet will evaluate in cells F4, F5, and F6:
To finish off the arguments for OFFSET, we provide cols as zero, because we want to stay in the same column. We provide height as 5 because we want a range that contains 5 rows. Finally, we provide width as 1, because we want a range that contains 1 column. After the row logic runs, this is how OFFSET works F4, F5, and F6:
The final step in the problem is just to sum the ranges provided by OFFSET. This is done with the SUM function. After OFFSET is evaluated, the resulting range is delivered directly the SUM function, which sums the values in the range and returns a final result. As the formula is copied down into F4:F6, we get the final totals per week:
To recap: OFFSET returns calculates the correct range for each week using the row logic explained above. The SUM function sums the ranges returned by OFFSET.