简体   繁体   English

Excel:如何对相同数字的单元格旁边的单元格求和

[英]Excel: How to sum up cells that are next to cells of the same number

I have two columns of data.我有两列数据。 The first column contains categories in the form of numbers, and the second column contains data that I want to sum up.第一列包含数字形式的类别,第二列包含我要汇总的数据。 So if you see the attached picture, I'd like to sum up all the red, green and blue into a single row, because each group shares the same numbered neighbor in the left column.因此,如果您看到所附图片,我想将所有红色、绿色和蓝色汇总到一行中,因为每个组在左列中共享相同编号的邻居。

I tried using SUMIF but that doesn't seem to be designed for this.我尝试使用 SUMIF 但这似乎不是为此而设计的。

例子

You are definitely barking up the right tree with SUMIF.你肯定是在用 SUMIF 咆哮正确的树。

Where: =SUMIF(VALUES for condition, Condition, NUMBERS to Sum)其中: =SUMIF(条件的值,条件,求和的数字)

Take a look at the attached example I put together which I believe is what you are after:看看我放在一起的附加示例,我相信这就是您所追求的:

SUMIF Example SUMIF 示例

Remember to lock the appropriate cells, in my case the VALUES for condition and NUMBERS to sum so that I can drag the formula which updates the Condition cell.请记住锁定适当的单元格,在我的情况下,条件和数字的值求和,以便我可以拖动更新条件单元格的公式。

You could use column C to calculate a cumulative sum, and reset whenever the value in column A changes.您可以使用 C 列来计算累积总和,并在 A 列中的值发生变化时重置。 So, you would:所以,你会:

  • set C1 to =B1将 C1 设置为=B1
  • set C2 to =IF(A1=A2, C1+B2, B2)将 C2 设置为=IF(A1=A2, C1+B2, B2)
  • copy C2 down to the end of the range将 C2 复制到范围的末尾

Now you can apply conditional formatting on column C to highlight the final sum when the next cell in column A is different, or you could make column D where:现在,当 A 列中的下一个单元格不同时,您可以在 C 列上应用条件格式以突出显示最终总和,或者您可以将 D 列设为:

  • set D1 to =IF(A1=A2, "", C1)将 D1 设置为=IF(A1=A2, "", C1)
  • copy D1 down to end of the range将 D1 向下复制到范围的末尾

This gives just the final sums, and empty cells for everything else.这仅给出最终总和,以及其他所有内容的空单元格。 You can hide column C, since it's an intermediate result.您可以隐藏 C 列,因为它是一个中间结果。

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

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