Excel公式只要求和

时间:2017-12-17 22:29:03

标签: excel excel-formula

我正在尝试找到一个公式来计算列的余额,直到找到负值。找到负值后,必须再次计算余额,直到下一个负值。基本上跟踪你花费的东西,除了它只在你卖东西时显示一个值。任何人都知道如果可以在MS excel中做到这一点吗?谢谢!

4 个答案:

答案 0 :(得分:0)

一种方法是添加2个额外的列,可以隐藏或在另一个工作表上,然后(这里假设添加了在右侧添加的列F2和G2):

In Cell E2: =IF(B2<0,G2,"")  
In Cell F2: =B2*C2  
In Cell G2: =SUM(F$2:F2)

将这些复制下来,假设我正确理解了您的问题,您将获得所需的结果。

答案 1 :(得分:0)

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.

答案 2 :(得分:0)

回应@carol,并再次查看Q&amp; A,特别是在你说“虽然问题出现之后,因为它需要忽略José之前的余额,并从后续的新的开始”我意识到您可能希望显示自上次销售以来所有销售收据和购买的余额。如果是这样的话:

In Cell E2: =IF(B2<0,G2,"")  
In Cell F2: =B2*C2  
In Cell G3: =IF(B2<0,F3,F3+G2)

不要复制以上内容。请复制下面的内容。

function flatArray($fullArray = array())
{
    $elements = array();

    foreach($fullArray as $key => $value) {
        if (is_array($value)) {
            $elements[] = $this->flatArray($value); 
        } else {
            $elements[] = sprintf('%s%s', $key, $value);
        }
    }

    return $elements;
}

答案 3 :(得分:0)

行。现在我得到你的问题。我认为以下内容可以解决问题。结果与您的示例完全匹配。

// 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)

我会注意到有关这方面的一些事情:

1)您可以考虑将列的名称更改为trans,quan,$ per,$ ttl,$ gp,并为我添加$ inv和inv的2列命名。
2)这是使用每次交易重新计算的库存平均成本,而不是LIFO或FIFO 3)如果条目失序使得quan变为负数,我认为这个解决方案将失败。无论如何,这可能是您想要注意的错误 4)仅供参考,&#34; IF(G2 = 0&#34; F3的一部分仅用于避免当G2(库存)为0时除以0错误。当然,我可以通过其他方式做到这一点。作品。
5)我假设你不能卖掉,因此我把E2留空了。