简体   繁体   中英

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!

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM