Offset Sum with Variable formula depending on row.

Scotster

Board Regular
Joined
May 29, 2017
Messages
59
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I've no idea how to describe what I'm looking for in a heading, so hopefully the below explains it a little better. Is there a more elegant way of calculating the below.

The formula I have in D6 is as follows:

Code:
=IF($C6="Net",SUM(OFFSET($D6,MATCH(1,($C1:$C6="Demand")*($B1:$B6=$B6)*($A1:$A6=$A6),0)-6,0):$D5),IF($C6="Demand",-10,5))

It's copied from D4 to D16, where D4 and D5 will REF error for the "Net" true calculation, but doesn't affect anything.

My aim is to replace the "-10" and "5" with a formula from another sheet. The reason for the convoluted method is there can be multiple Suppliers, so some parts cover 3 lines or more. The offset only allows for 6 lines at the maximum so I was hoping there would be a better option.

1679588489907.png


What I want is for the demand line to use one calculation, the net line to use another calculation and the remaining supply lines to use a 3rd calculation. There are 10000s of rows, so creating one formula to copy down the entire sheet I feel is beneficial, rather than using VBA to determine the individual formulae per row. The difficulty with the "Net" calculation, is that it's simply a sum of the rows above it..... up to and including the "Demand" row. With the "Demand" row varying in distance I've used the match to find the correct offset.

As always, any help gratefully appreciated :)
 
i don't know why that put a diff formula in row 14 hopefully this doesn't:

mr excel questions 20.xlsm
ABCDE
1MaterialLocationDemand/Sup/Net
2Part 1Loc 1Demand15
3Part 1Loc 1Supp 1-5
4Part 1Loc 1Net10
5Part 1Loc 2Demand15
6Part 1Loc 2Supp 1-5
7Part 1Loc 2Supp 2-5
8Part 1Loc 2Net5
9Part 2Loc 1Demand15
10Part 2Loc 1Supp 1-5
11Part 2Loc 1Net10
12Part 3Loc 1Demand15
13Part 3Loc 1Supp 1-5
14Part 3Loc 1Net10
15
Scotster
Cell Formulas
RangeFormula
D2:D14D2=IF($C2="Demand",15,IF($C2<>"Net",-5,SUM(INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1):$D1)))
 
Upvote 0
Solution

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
i think this is it:
mr excel questions 20.xlsm
ABCDE
1MaterialLocationDemand/Sup/Net
2Part 1Loc 1Demand15
3Part 1Loc 1Supp 1-5
4Part 1Loc 1Net10
5Part 1Loc 2Demand15
6Part 1Loc 2Supp 1-5
7Part 1Loc 2Supp 2-5
8Part 1Loc 2Net5
9Part 2Loc 1Demand15
10Part 2Loc 1Supp 1-5
11Part 2Loc 1Net10
12Part 3Loc 1Demand15
13Part 3Loc 1Supp 1-5
14Part 3Loc 1Net10
15
Scotster
Cell Formulas
RangeFormula
D2:D13D2=IF($C2="Demand",15,IF($C2<>"Net",-5,SUM(INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1):$D1)))
D14D14=IF($C14="Demand",10,IF($C14<>"Net",-5,SUM(INDEX($D$2:$D$14,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C16))))*(ROW($C$2:$C16)-1))),1):$D15)))

That looks to be ideal, just thinking about how it works. Will try it out tomorrow and see how it works with the actual sheet.

Many thanks for helping out and getting back so quickly :)
 
Upvote 0
I used 15 and -5 in my testing. I hope you can figure it out.
It also avoid using the OFFSET function, which is volatile and can slow your worksheets to a crawl if its big.

Also, it is using array formulas, you may need to enter them with CNTRL-SHFT-ENTER

The INDEX function can be used to build range references and that is what this is doing.

Note, I posted an updated mini worksheet. the original had a different formula in row 14 and I wanted it removed.
 
Upvote 0
Here is what the formula does:

SUM(INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1):$D1)

The red part is creating a cell reference that is located on the last Demand above the cell the formula is in.

INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1)

The red part here is making an array of 1s and 0s, where if the word demand is in a cell it will have a one and if not it will have a zero

INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1)

The red part here is creating and array of numbers beginning with the number 1, with the number 1 being the first row of the range identified in the first part of the matrix formula.

The two arrays above are then multiplied together to get the row number in the INDEX range where the word Demand is on that row.

If you notice the INDEX range is a dynamic going only to the row above the current cell. And since you want to start with the demand closest to this row,… we get the maximum of the multiplied array. This identifies the row in the INDEX range to use.

The last argument of the INDEX formula portion is just the number 1, indicating the column number.

In addition to return values from a range INDEX can be used to calculate a cell reference when paired with a colon after the function, so by referencing the cell above the formula cell (in this case formula is in $D2, so the end reference is $D1.

Finally, its wrapped up with the SUM function.

Since this formula uses array formulas, you may need to use the CNTL-SHFT-ENTER method to tell excel “Hey process the arrays here!”.
 
Upvote 0
Here is what the formula does:

SUM(INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1):$D1)

The red part is creating a cell reference that is located on the last Demand above the cell the formula is in.

INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1)

The red part here is making an array of 1s and 0s, where if the word demand is in a cell it will have a one and if not it will have a zero

INDEX($D$2:$D2,SUM(MAX((--(ISNUMBER(FIND("Demand",$C$2:$C2))))*(ROW($C$2:$C2)-1))),1)

The red part here is creating and array of numbers beginning with the number 1, with the number 1 being the first row of the range identified in the first part of the matrix formula.

The two arrays above are then multiplied together to get the row number in the INDEX range where the word Demand is on that row.

If you notice the INDEX range is a dynamic going only to the row above the current cell. And since you want to start with the demand closest to this row,… we get the maximum of the multiplied array. This identifies the row in the INDEX range to use.

The last argument of the INDEX formula portion is just the number 1, indicating the column number.

In addition to return values from a range INDEX can be used to calculate a cell reference when paired with a colon after the function, so by referencing the cell above the formula cell (in this case formula is in $D2, so the end reference is $D1.

Finally, its wrapped up with the SUM function.

Since this formula uses array formulas, you may need to use the CNTL-SHFT-ENTER method to tell excel “Hey process the arrays here!”.
gahhhh. my grammar and spelling is horrible, i hope you can decipher.
 
Upvote 0
I can confirm it works exactly as intended. Thanks very much for the help. It makes it far less cumbersome to use the Max to find the last demand point :)
 
Upvote 0
I can confirm it works exactly as intended. Thanks very much for the help. It makes it far less cumbersome to use the Max to find the last demand point :)
Happy to help!

Best wishes.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top