我试图在Excel中对两行中具有相同文本值的行进行求和。我看到这里的帖子Group by Sum in Excel关于在一列中对具有相同文本值的行求和。这是我的表:
A B C
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
我想我需要使用Sumifs而不是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!
答案 0 :(得分:1)
试试这个公式:
=IF(OR(B2<>B3,A2<>A3),SUMIFS($C$2:$C$10,$A$2:$A$10,A2,$B$2:$B$10,B2),"")
警告说,必须对列进行排序。如果未对列进行排序,则无效。
答案 1 :(得分:0)
我发现效果很好的替代方法是添加一个列,其中包含您希望将哪些列分组为单个值。例如,设置col D:
=A1&"_"&B1
result is A_South etc
然后,您可以使用常规SUMIF,使用此列作为条件。
主要好处是公式的分离,您可以轻松扩展到进一步的分组变量。
答案 2 :(得分:0)
您可以使用UDF执行此操作:
=SumQuatitySold(2;A2;B2;C:C)
其中:
您只需在模块中输入此代码:
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