Fetch SUM of columns above column - excel

I am building an excel formula, where i want to do something like below
Suppose i have 10 rows in excel sheet
I have values in B1 to B9, i want to create a formula so that sum is shown in C10.
I know it is simple =SUM(B1-B10) but only in case when number of rows are fixed.
Whereas in my case, i also want this relation to work if i keep on adding any number of rows, so
C(n) should always give a sum of B(1) to B(n-1)
I have tried indirect but not able to use it convincingly

Try this in C10
=SUM(OFFSET(B$1,0,0,ROW()-1,1))

Assuming you fix C1=0, the formula you should use is
[C2] =OFFSET(C2,-1,-1)+OFFSET(C2,-1,0)
and fill down.

Related

How to simplify 64 levels of nesting formula in Excel?

I encountered "64 levels of nesting" issue while working on my formula. Is there any way to simplify this formula? This formula worked fine up from 10 to 500 (G:G) but once over 510 something, it encountered "64 levels of nesting" issue. I found some solutions that involved lookup and match-index, but I can't see where/how to implement it here.
Formula is in cell B3:
=IF(B2<=$G$2,B2+($E$2*B2),IF(AND(B2>$G$2,B2<=$G$3),B2+($E$3*B2),IF(AND(B2>$G$3,B2<=$G$4),B2+($E$4*B2),IF(AND(B2>$G$4,B2<=$G$5),B2+($E$5*B2),IF(AND(B2>$G$5,B2<=$G$6),B2+($E$6*B2),IF(AND(B2>$G$6,B2<=$G$7),B2+($E$7*B2),IF(AND(B2>$G$7,B2<=$G$8),B2+($E$8*B2),IF(AND(B2>$G$8,B2<=$G$9),B2+($E$9*B2),IF(AND(B2>$G$9,B2<=$G$10),B2+($E$10*B2),IF(AND(B2>$G$10,B2<=$G$11),B2+($E$11*B2),IF(AND(B2>$G$11,B2<=$G$12),B2+($E$12*B2),IF(AND(B2>$G$12,B2<=$G$13),B2+($E$13*B2),IF(AND(B2>$G$13,B2<=$G$14),B2+($E$14*B2),IF(AND(B2>$G$14,B2<=$G$15),B2+($E$15*B2),IF(AND(B2>$G$15,B2<=$G$16),B2+($E$16*B2),IF(AND(B2>$G$16,B2<=$G$17),B2+($E$17*B2),IF(AND(B2>$G$17,B2<=$G$18),B2+($E$18*B2),IF(AND(B2>$G$18,B2<=$G$19),B2+($E$19*B2),IF(AND(B2>$G$19,B2<=$G$20),B2+($E$20*B2),IF(AND(B2>$G$20,B2<=$G$21),B2+($E$21*B2),IF(AND(B2>$G$21,B2<=$G$22),B2+($E$22*B2),IF(AND(B2>$G$22,B2<=$G$23),B2+($E$23*B2),IF(AND(B2>$G$23,B2<=$G$24),B2+($E$24*B2),IF(AND(B2>$G$24,B2<=$G$25),B2+($E$25*B2),IF(AND(B2>$G$25,B2<=$G$26),B2+($E$26*B2))))))))))))))))))))))))))
Cells $A2:$A26 & cells $E2:$E26 are variable. Cells $D2:$D26 are just my indicator. Others are constant value.
Cells $B2:$B26 will calculate the formula $A2+$A2* percentage. The percentage is based on the value which is in cells $E2:$E26. The value is then determine by the value in cells $A2:$A26.
Formula in cells $B2:$B26 are as previous formula dragged down.
Example:
If the cost is 50. The value of percentage will be taken from cell $E$7. In this case is 10 thus returning the value in $B$6 to $A6+$A6*10=55
Please refer to ss 2.
excel screenshot 2
If you reach the nesting limit that nearly always means there's an easier way....
You should be able to do this much more simply with a lookup type formula, e.g.
=B2+B2*IF(B2<G$2,E$2,INDEX(E$3:E$26,MATCH(TRUE,INDEX(B2>G$2:G$25,0),0))
This finds the first value in column G which is > B2 and then gets the required value from the next row in column E
The only thing this formula doesn't do is impose an upper limit on B2 (yours is the G26 value 250). If that's an issue you can just add an extra IF to cater for that
You might want to rework your tables but an INDEX/MATCH with a relative lookup on ascending data will return the correct percentage.
=b2*index(e2:e26, match(b2, g2:g26, 1))
Not 100% sure I understand your formula/intent. I think I got it...if not please clarify.
add another column that does the math in column H. enter this formula and drag down.
=($B$2+(E2*$B$2))
in cell B3 enter this formula
=VLOOKUP(ROUNDUP(B2,-1),G:H,2,FALSE)

Excel formula to sum as long as

I'm trying to find a formula to calculate the balance of a column until a negative value is found. After the negative value is found, the balance must be calculated again until the next negative value. Basically tracking what you spent, except it only shows a value when you sold something. Anybody have an idea if this is possible to do in MS excel? Thanks!
OK. Now I get your question. I think the following will do the trick. The results exactly match your example.
// In these cells only
F2: =MAX(0,B2*C2)
G2: =MAX(0,B2)+MIN(0,B2)
// In these cells, then copy down
E3: =IF(B3<0,D3-(F2-F3),"")
F3: =F2+MAX(0,B3*C3)+IF(G2=0,0,MIN(0,B3*F2/G2))
G3: =G2+MAX(0,B3)+MIN(0,B3)
I would note a couple of things about this:
1) You might consider changing the names of your columns to trans, quan, $ per, $ ttl, $ gp, and name the 2 columns I am adding $ inv and inv.
2) This is using the average cost of inventory, recalculated with each transaction, not LIFO or FIFO.
3) If entries get out of order such that quan goes negative, I think this solution will fail. In any case, that might be an error you'd want to notice.
4) FYI, the "IF(G2=0" part of F3 is only there to avoid a divide by 0 error when G2 (inventory) is 0. I could have done this other ways, of course. It works.
5) I've left E2 blank on the assumption that you can't sell as you've not bought.
One way would be to add 2 additional columns, which could be hidden or on another sheet, then (here assuming adding columns F2 and G2 added at the right):
In Cell E2: =IF(B2<0,G2,"")
In Cell F2: =B2*C2
In Cell G2: =SUM(F$2:F2)
Copy these down and, assuming I understand your question correctly, you'll get the results you desire.
The biggest problem you've got with this is working out the ranges to check for the balance calculation. This won't work if the next row in your table is a 'sell' while you've still got one 'apple' left from the previous purchase. If you want to do anything more convoluted you should use VBA.
Others will probably have an easier way to work out the ranges; I did it in a rather convoluted way and don't have time to optimise them.
In column F there's an array formula to calculate the last row in the range of 'buys' relevant to that 'sell'.
=IF($B2>=0,"",LARGE(IF($B$2:$B2>0,ROW($A$2:$A2)),1))
In column G there's a normal formula to capture the row number of the first row in the range.
=IF(ROW()=2,ROW(),IF(B2>0,IF(B1<0,ROW(),""),""))
In column H, convert this to be stored in the relevant 'sell' row using an array formula:
=IF($B2>=0,"",LARGE(IF($G$2:$G2>0,$G$2:G2),1))
In column E, balance, use these calculated row range references in an INDIRECT statement to calculate the balance.
=IF(B2>0,"",(C2*-(B2))-(-(B2)*(SUMPRODUCT(INDIRECT("B"&H2&":B"&F2),INDIRECT("C"&H2&":C"&F2)/SUM(INDIRECT("B"&H2&":B"&F2))))))
Note that INDIRECT uses a string to reference cells and ranges and if you move cells or ranges you will have to manually change the INDIRECT string to reference the correct cells.
Responding to #carol, and looking at the Q&A again, specifically where you say " although the problems comes up after because it needs to ignore the balances that came before José and start with the new ones that follow up," I realize that you may be looking to instead display the balance of all sales receipts and purchases since the last sale. If so:
In Cell G2: =F2
Do not copy down the above. Do copy down those below.
In Cell E2: =IF(B2<0,G2,"")
In Cell F2: =B2*C2
In Cell G3: =IF(B2<0,F3,F3+G2)

Excel: How to apply a formula to multiple cells while changing only one parameter?

I'm trying to apply an INDEX/MATCH formula to multiple cells, and the only parameter I want to change in between cells is the "col_number". It this possible?
Example:
=INDEX(Sheet2!$A1:$APH344,MATCH(Sheet1!$A2,Sheet2!$A:$A,0),2)
In the next cell over, I would like to keep everything equal, but change that last "2" to "3":
=INDEX(Sheet2!$A1:$APH344,MATCH(Sheet1!$A2,Sheet2!$A:$A,0),3)
Then:
=INDEX(Sheet2!$A1:$APH344,MATCH(Sheet1!$A2,Sheet2!$A:$A,0),4)
And so on..
Is there any way to apply this pattern to many cells (i.e. 100s of cells) quickly? Thanks!
You are not using $ in your rows.
Give this a try:
INDEX(Sheet2!$A$1:$APH$344,MATCH(Sheet1!$A$2,Sheet2!$A:$A,0),2)
As Ahmed pointed out, the dollar sign $ is placed before both the column and row, when you don't want it to change as you drag a formula to other cells.
You can use the ROW() function, instead of a static numerical value, to increase the column number as you fill down the column.Here is your original formula using the ROW() function instead : =INDEX(Sheet2!$A1:$APH344,MATCH(Sheet1!$A2,Sheet2!$A:$A,0),ROW()).
If your formula does not start in row one, then you will need to subtract the number of rows that it is removed from row one.For example, if your formula started in row 8: ROW()-ROWS(A$1:A7).
That depends on which way you drag your formula,
Dragging from top to bottom:
=INDEX(Sheet2!$A1:$APH344,MATCH(Sheet1!$A2,Sheet2!$A:$A,0),ROW(2:2))
Dragging from left to right,
=INDEX(Sheet2!$A1:$APH344,MATCH(Sheet1!$A2,Sheet2!$A:$A,0),COLUMN(B:B))
If you want a cell to be fix, use $ to fix the column and or the row.
For example, to fix the B4 cell, use $B$4 instead. After that just drag the formula to the other cells

Need help combining formulas into a multiple IF statement

I'm having trouble writing a formula that will work and was hoping someone could offer assistance with something that requires multiple IF's.
Formula in J4 and J6: =($A$2-F6+1)/($A$2-DATE(YEAR($A$2),1,0))
Formula in J5: =(G5-DATE(YEAR(G5),1,0))/(A2-DATE(YEAR(A2),1,0))
What I want is a formula that can combine the two above formulas, with a max of 1.
IF Column G is Blank, then I want the formula from J4 and J6 with the number capped at 1.
IF Column G is NOT Blank, then my formula from J5.
I keep getting errors whenever I attempt to write this with multiple IF's in the same statement.
I want one formula that would yield 1 on J4, .344444 on J5, and .65555 on J6.
Thank you for your help!
Use this:
=MIN(1,IF(G4<>"",(G4-DATE(YEAR(G4),1,0))/($A$2-DATE(YEAR($A$2),1,0)),($A$2-F4+1)/($A$2-DATE(YEAR($A$2),1,0))))

Dynamical SUMIF command

If it is possible, I'd like to create a formula that will allow me the following:
Formula must be in the entire column or in this example, in the range B1:B5. The formula is based on a condition, that when the total sum of cells from column A is lower D1, than it gives "X". If the total sum is over D1, then it gives an empty field - "".
In this example the total sum of the cells, that are over D1 value is in the first 3 rows, hence the three X-es, and then it stops.
(source: shrani.si)
.
I presume it would be possible to do this with multiple SUMIF commands, but does anyone know a smoother solution for this?
Thanks!
You can do this easily by using an absolute reference for the starting point of a SUM and using a relative reference for the end point. When copied down, this formulas works fine.
=IF(SUM($A$1:A1)<$D$1,"x","")
Results
Try this
=IF(SUM(OFFSET(A1,0,0,$A$1:A1,1))>$D$1,"X","")
This formula should start at B1 and then you use auto-increment to populate other cells

Resources