OFFSET function And the Sum formula
About : To calculate the total of a specific group of months the =OFFSET() function has been used.
Category : Lookup
Location : Built-In
Sometimes it is necessary to base a calculation on a set of cells in different locations.
An example would be when a total is required from certain months of the year, such as the last 3 months in relation to the current date.
One solution would be to retype the calculation each time new data is entered, but this would be time consuming and open to human error.
A better way is to indicate the start and end point of the range to be calculated by using the =OFFSET() function.
The =OFFSET() picks out a cell a certain number of cells away from another cell.
By giving the =OFFSET() the address of the first cell in the range which needs to be totaled, we can then indicate how far away the end cell should be and the =OFFSET() will give us the address of cell which will be the end of the range to be totaled.
The =OFFSET() needs to know three things;
- A cell address to use as the fixed point from where it should base the offset.
- How many rows it should look up or down from the starting point.
- How many columns it should look left or right from the starting point.
=SUM(D3:OFFSET(D3,0,0))
This example uses D3 as the starting point and offsets no rows or columns which
results in the range being summed as D3:D3.
=SUM(D3:OFFSET(D3,0,1))
This example uses D3 as the starting point and offsets 1 col to pick out cell D3 resulting in a the range D3:D3 being summed.
=SUM(D3:OFFSET(D3,0,2))
This example uses D3 as the starting point and offsets 2 cols to pick out cell D3 resulting in a the range D3:D3 being summed.
Using =OFFSET() Twice In A Formula
The following examples use =OFFSET() to pick both the start and end of the range which needs to be totaled.
=SUM(OFFSET(E45,0,1):OFFSET(E45,0,1))
The cell D3 has been used as the starting point for both offsets and each has been offset by just 1 column. The result is that just cell E3 is used as the range E3:F3 for the sum function to calculate.
=SUM(OFFSET(D3,0,1):OFFSET(D3,0,2))
The cell D3 has been used as the starting point of both offsets, the first offset is offset by 1 column, the second by 2 columns. The result is the range E3:F3 which is then totaled.
=SUM(OFFSET(D3,0,1):OFFSET(D3,0,3))
The cell D3 has been used as the starting point for both offsets, the first offset is offset by 1 column, the second by 3 columns. The result is the range E3:G3 which is then totaled.
Lets understand with some more examples
The following table shows five months of data.
To calculate the total of a specific group of months the =OFFSET() function has been used.
The Start and End dates entered in cells E2 and E3 are used as the offset to produce a range which can be totaled.
=SUM(OFFSET(C10,0,MONTH($E$2)):OFFSET(C10,0,MONTH($E$3)))
Explanation
The following formula represent a breakdown of what the =OFFSET function does.
The formula displayed below are only dummies, but they will update
as you enter dates into cells E2 and E3.
Formula 1.
=SUM( OFFSET(C10,0,MONTH(E2)) : OFFSET(C10,0,MONTH(E3)) )
This is the actual formula entered by the user.
Formula 2.
=SUM(OFFSET(C10,0,MONTH(2)) : OFFSET(C10,0,MONTH(3)))
="=SUM( OFFSET(C10,0,MONTH("&TEXT(MONTH($E$2),"0")&")) : OFFSET(C10,0,MONTH("&TEXT(MONTH($E$3),"0")&")) )"
This shows how the =MONTH function calculates the month number.
In this example the values of the months are 2 and 3 for Feb and Mar.
These values are the 'offsets' relative to cell C10.
Formula 3.
=SUM(OFFSET(C10,0,2) : OFFSET(C10,0,33))
="=SUM( OFFSET(C10,0,"&TEXT(MONTH($E$2),"0")&") : OFFSET(C10,0,"&TEXT(MONTH($E$3),"0")&") )"
This shows where the month numbers are used in the =OFFSET function.
Formula 4.
=SUM(E10:F10)
=SUBSTITUTE("=SUM( "&@CELL("address",OFFSET(C10,0,MONTH($E$2)))&":"&@CELL("address",OFFSET(C10,0,MONTH($E$3)))&" )","$","")
This shows how the =OFFSET eventually equates to cell addresses to be used as a range for the =SUM function.









No comments:
Post a Comment