尝试使用SUMIF按类别对数据进行分组:
我有一个看起来像这样的数据集:
我还有一个属性列表:
现在,我希望随着时间的流逝,v-show="user && checkUserRole(item)"
Class A
和Class B
之和。
因此,在新列Class C
中输入了公式:(Cell I2)
,结果应为=SUMIF($B$1:$H$1,$N$2:$N$8,$B2:$H2)
,但不是吗?
我在做什么错了?
答案 0 :(得分:1)
将单元格I1到K1中的标签更改为A,B和C。
然后您可以在单元格I2中使用
=SUMPRODUCT($B2:$H2,--(INDEX($O$2:$O$8,MATCH($B$1:$H$1,$N$2:$N$8,0))=I$1))
上下复制。此方法在具有新的动态数组功能的Excel 365中有效,当前仅对Insider版本可用。此屏幕截图中的证明:
对于旧版本的Excel,最简单的方法是在公司名称下方插入一行,然后在其中添加类。然后,您可以使用一个简单的Sumifs of Sumifs公式。这是Excel 2010屏幕截图中使用的Sumifs公式:
=SUMIFS($B3:$H3,$B$2:$H$2,I$1)
编辑两项:感谢Tim Heng提供的公式可以输入到单元格I2中,上下复制。在任何版本的Excel中均可使用。
=SUMPRODUCT(($B$1:$H$1=$N$2:$N$8)*($O$2:$O$8=I$1)*$B2:$H2)
答案 1 :(得分:1)
在I2单元格中,使用以下公式:
=SFive($B2:$H2,$B$1:$H$1,$N$2:$N$8,$I$1:$K$1,$O$2:$O$8)
并在需要的地方复制/粘贴。
Function SFIVE(SumRange As Range, CompanyH As Range, CompanyV As Range, _
ClassH As Range, ClassV As Range) As Double ' As Long if whole numbers.
Dim vntSumH As Variant ' Horizontal Sum Array
Dim vntCompH As Variant ' Horizontal Company Array
Dim vntCompV As Variant ' Vertical Company Array
Dim vntClassH As Variant ' Horizontal Class Array
Dim vntClassV As Variant ' Vertical Class Array
Dim strClass As String ' Current Class
Dim strCompany As Variant ' Current Company
Dim j As Long ' Horizontal Class Range Column Counter
Dim i As Long ' Vertical Class and Vertical Company Array Row Counter
Dim k As Long ' Horizontal Company and Sum Array Row Counter
vntSumH = SumRange ' Copy Horizontal Sum Range to array.
vntCompH = CompanyH ' Copy Horizontal Company Range to array.
vntCompV = CompanyV ' Copy Vertical Company Range to array.
vntClassH = ClassH ' Copy Horizontal Class Range to array.
vntClassV = ClassV ' Copy Vertical Class Range to array.
' Loop through columns of Horizontal Class Range.
For j = 1 To ClassH.Columns.Count
' Check column of "Input Cell" against column of Horizontal Class Range.
If Application.ThisCell.Column = ClassH.Column + j - 1 Then
' Assign only Class (last character) to Current Class.
strClass = Right(vntClassH(1, j), 1)
' Loop through rows of Vertical Class (Company) Array.
For i = 1 To UBound(vntClassV)
' Check if value of current row in Vertical Class Array
' is equal to Current Class.
If vntClassV(i, 1) = strClass Then
' Assign value of current row in Vertical Company Array
' to Current Company.
strCompany = vntCompV(i, 1)
' Loop through columns of Horizontal Company (Sum) Array.
For k = 1 To UBound(vntCompH, 2)
' Check if value of current column in Horizontal
' Company Array is equal to Current Company.
If vntCompH(1, k) = strCompany Then
' Check if current column in Sum Array contains
' a number.
If IsNumeric(vntSumH(1, k)) Then
' Add value of current column in Sum Array
' to SFIVE.
SFIVE = SFIVE + vntSumH(1, k)
End If
' If you will allow multiple occurrences of
' a Company in Horizontal Company Array (Range),
' you will have to outcomment the following
' "Exit For" line.
Exit For
End If
Next
End If
Next
Exit For ' Stop searching because these are unique values.
End If
Next
End Function