简体   繁体   English

excel公式,可根据条件在单行中添加单元格

[英]excel formula to add cells in a single row based on criteria

Let's say, 比方说

Sheet1:A1 = x

Sheet2 contents look like Sheet2的内容看起来像

x 3 3 4 
y 0 2 1

Is there an excel formula that could match Sheet1:A1 with Sheet2:A1 (basically the value 'x') and add the other cells in that row (3,3,4). 是否存在一个可以将Sheet1:A1与Sheet2:A1(基本上是值“ x”)匹配并在该行中添加其他单元格(3,3​​,4)的Excel公式。 The result (Sum='10') should get updated at Sheet1:C4 lets say. 结果(Sum = '10')应该在Sheet1中更新:C4可以说。

I tried SUMIF but that shows the content of only one column due to the restriction that it can handle only the matchable array size. 我尝试了SUMIF,但是由于它只能处理可匹配的数组大小的限制,因此只能显示一列的内容。 I know this can be achieved through VBA, but just wanted to know if a formula is available. 我知道这可以通过VBA来实现,但是只是想知道是否有公式可用。

TIA! TIA!

This formula will do what you want: 该公式将满足您的要求:

=SUM(SUMIF(Sheet2!A:A,A1,INDIRECT("Sheet2!" & {"B:B","C:C","D:D"})))

It will iterate through the columns doing individual SUMIF() on each and then adding the results. 它将遍历各列,对每个列进行单独的SUMIF(),然后添加结果。

If you want more columns or different change the address in the array to the columns desired. 如果需要更多列或其他列,请将数组中的地址更改为所需的列。

在此处输入图片说明

Try the following 尝试以下

=SUM(IF(Sheet2!A1:A99=A1,Sheet2!B1:D99,0))

Note that this is an array formula, so it must be entered using Ctrl + Shift + Enter . 请注意,这是一个数组公式,因此必须使用Ctrl + Shift + Enter输入

What this formula does is converts any rows on Sheet2 without x in column A to zeros in B:D and then sums what is left. 该公式的作用是将Sheet2 A列中没有x任何行转换为B:D零,然后求和。

Similarly, you could use 同样,您可以使用

=SUMPRODUCT((Sheet2!A1:A99=A1)*Sheet2!B1:D99)

and you wouldn't have to enter it as an array formula. 而且您不必将其作为数组公式输入。

对于非易失性,非数组公式,请尝试以下操作

=SUM(INDEX(Sheet2!B:D,MATCH(Sheet1!A1,Sheet2!A:A,0),))

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

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