如何使用另一列中的多个条件进行SUMIFS文本匹配?
我希望以我可以即时更改类别的方式对交易进行分类。 我觉得通过展示我所拥有的东西来解释起来更简单。
我有以下表格
交易
- 一个约会
- C:姓名
- D:金额
快餐名单:
- L:名字(部分名字,因为要做stringsearch)
我希望根据多个标准(如date和类别)对交易金额进行汇总。 这是一个可行的公式:
=SUMIFS(D:D,A:A,"*03/2013*",C:C,"*"&L3&"*")
有一个基本问题:只支持快餐单中的一项。 有什么办法可以简单地在整个快餐名称上进行文字串search? “ ”&L3&“ ”到“ ”&L:L&“ ”什么的?
这是我尝试过的一些事情。
1)用布尔UDF修改SUMIFS标准“ ”&L3&“ ”。 我遇到的问题是,我不知道如何通过SUMIF循环当前行到函数中。
Public Function checkRange(Check As String, R As Range) As Boolean For Each MyCell In R If InStr(Check, MyCell.Value) > 0 Then checkRange = True End If Next MyCell End Function
如果我可以发送检查到这个function,那么我会被设置。
2)将SUMIFS的sum_rangereplace为返回行范围的UDF
Public Function pruneRange(Prune_range As Range, Criteria_range As Range) As Range Dim Out_R As Range Dim Str As String ActiveWorkbook.Sheets("Vancity Trans").Activate ' Loop through the prune_range to make sure it belongs For Each Cell In Prune_range ' loop through criteria to see if it matches current Cell For Each MyCell In Criteria_range If InStr(Cell.Value, MyCell.Value) > 0 Then ' Now append cell to Out_r and exit this foreach ' Str = Str & Cell.Address() & "," Str = Str & "D" & Cell.Row() & "," Exit For End If Next MyCell Next Cell ' remove last comma form str Str = Left(Str, Len(Str) - 1) ' use str to set the range Set Out_R = Range(Str) ' MsgBox (Str) Set pruneRange = Out_R End Function
这适用于常规的SUM循环,但由于某种原因,当我尝试在SUMIF或SUMIFS中使用它时,它会返回#Value。 另一个问题是,即使在SUM循环中,如果使用C:C而不是C1:CX,其中X是多行,它会崩溃excel或永久循环。 我猜这是因为除非我告诉它,excel不知道何时停止在UDF中?
试试这个公式
=SUMPRODUCT(SUMIFS(D:D,A:A,"*03/2013*",C:C,"*"&L3:L30&"*"))
通过使用范围(L3:L30)作为最终标准,SUMIFS公式将生成一个“数组”(28个值 – 每个L3中的值:L30)… SUMPRODUCT用于求和数组结果你想要的