在具有相同文本值的Excel中汇总行

我想要总结Excel中具有2个不同列中相同的文本值的行。 我在Excel中看到postSum by Excel在一列中将相同文本值的行相加。 这是我的桌子:

ABC Item Region quantity_sold A South 1 A South 4 A North 7 B South 5 B South 9 D South 3 C South 6 C South 4 C North 8 

我想我需要使用Sumif而不是Sumif。 我想结果是:

 Item Region quantity_sold A South A South 5 A North 7 B South B South 14 D South 3 C South C South 10 C North 8 

我试过= IF(A2 = A1,“”,SUMIFS(A:A,A2,B:B,B2,C:C))但是我得到了:#VALUE!

另一个我觉得很好的方法是添加一个包含你希望分组的列作为单个值的列。 比如用这个设置col D:

 =A1&"_"&B1 result is A_South etc 

然后,您可以使用正常的SUMIF,使用此列作为条件。

主要好处是公式的分离,并且可以很容易地扩展到更多的分组variables。

试试这个公式:

 =IF(OR(B2<>B3,A2<>A3),SUMIFS($C$2:$C$10,$A$2:$A$10,A2,$B$2:$B$10,B2),"") 

在这里输入图像说明

警告,列必须sorting。 如果列未被sorting,这将不起作用。

你可以使用UDF来做到这一点:

 =SumQuatitySold(2;A2;B2;C:C) 

哪里:

  • 第一项是起始行,在这种情况下是2
  • 第二个是你的项目(select单元格)
  • 第三个是区域(select单元格)
  • 最后一个selectquantity_sold的列

为例

你只需要在模块中input这个代码:

 Function SumQuatitySold(StartRow As Long, Item As Range, region As Range, Quantity_Sold As Range) As Long Dim ws As Worksheet Dim i As Long, j As Long Set ws = ThisWorkbook.ActiveSheet With ws i = Item.Row j = StartRow Do If .Cells(i, Item.Column) = .Cells(j, Item.Column) And .Cells(i, region.Column) = .Cells(j, region.Column) And i <> j Then If quantitysold = 0 Then quantitysold = .Cells(j, Quantity_Sold.Column) Else quantitysold = quantitysold + .Cells(j, Quantity_Sold.Column) End If End If j = j + 1 Loop Until IsEmpty(.Cells(j, Quantity_Sold.Column)) End With SumQuatitySold = quantitysold + ws.Cells(Item.Row, Quantity_Sold.Column) End Function