简体   繁体   English

Excel公式求和只要

[英]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? 任何人都可以在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): 一种方法是添加2个额外的列,这些列可以隐藏或隐藏在另一张纸上,然后(这里假设在右边添加列F2和G2):

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. 如果您想做更多复杂的事情,则应使用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'. F列中有一个数组公式,用于计算与该“卖出”相关的“买入”范围内的最后一行。

=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. 在G列中,有一个正常公式可以捕获范围内第一行的行号。

=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: 在H列中,使用数组公式将其转换为存储在相关“出售”行中:

=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. 在E列余额中,在INDIRECT语句中使用这些计算的行范围引用来计算余额。

=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. 请注意,INDIRECT使用字符串来引用单元格和范围,如果移动单元格或范围,则必须手动更改INDIRECT字符串以引用正确的单元格。

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. 回复@carol,然后再次查看问答,特别是您说的地方:“尽管问题出在后面,因为它需要忽略José之前的余额并从后续的新余额开始,”我意识到您可能而是显示自上次销售以来所有销售收支的余额。 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. 1)您可以考虑将列的名称更改为trans,quan,$ per,$ ttl,$ gp,并命名我要添加$ inv和inv的2列。
2) This is using the average cost of inventory, recalculated with each transaction, not LIFO or FIFO. 2)这是使用库存的平均成本,而不是LIFO或FIFO,而是根据每次交易重新计算的。
3) If entries get out of order such that quan goes negative, I think this solution will fail. 3)如果条目混乱,导致quan变为负数,我认为此解决方案将失败。 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. 4)仅供参考,F3的“ IF(G2 = 0)”部分只是为了避免在G2(库存)为0时被0除以错误。我当然可以用其他方法做到这一点。
5) I've left E2 blank on the assumption that you can't sell as you've not bought. 5)我将E2留空,假设您没有买就不能卖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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