我想创建一个几乎与 SumIfs 完全相同的函数,但我很难弄清楚如何处理 ParamArray 部分。我正在寻找一种解决方案,它允许与 sum ifs 相同的 Range1,Criteria1,Range2,Criteria2,...,Ragen,Criterian 但在我的“SumIfsContains”函数中。我附上了单数案例的代码“SumIfContains”,这样您就可以看到我的起点:
Function SumIfContains(PhraseRange As Range, Criteria As String, SumRange As Range)
Dim element As Range
ElementCount = 0
For Each element In PhraseRange
ElementCount = ElementCount + 1
Next element
Dim SumArray: ReDim SumArray(1 To 3, 1 To ElementCount)
ElementCount = 0
For Each element In SumRange
ElementCount = ElementCount + 1
SumArray(2, ElementCount) = element
Next element
ElementCount = 0
For Each element In PhraseRange
ElementCount = ElementCount + 1
SumArray(1, ElementCount) = element
If InString(CStr(element), Criteria) Then
SumArray(3, ElementCount) = SumArray(2, ElementCount)
Else
SumArray(3, ElementCount) = 0
End If
Next element
SumIfContains = 0
For Item = 1 To ElementCount
SumIfContains = SumIfContains + CDbl(SumArray(3, Item))
Next Item
End Function
在我昨晚得到答案之前,我想出了一个工作选项如下:
Function SumIfsContains(SumRange As Range, ParamArray Criteria() As Variant)
Dim element As Range
Dim cCriteria As String
Dim PhraseRange As Range
'Exit Function
Dim PhraseRangeArray(): ReDim PhraseRangeArray(LBound(Criteria()) To (((UBound(Criteria()) + 1) / 2) - 1))
Dim CriteriaArray(): ReDim CriteriaArray(LBound(Criteria()) To (((UBound(Criteria()) + 1) / 2) - 1))
CurrentPair = 0
For i = LBound(Criteria()) To UBound(Criteria())
If i Mod 2 = 0 Then
PhraseRangeArray(CurrentPair) = Criteria(i)
Else
CriteriaArray(CurrentPair) = Criteria(i)
CurrentPair = CurrentPair + 1
End If
Next i
ElementCount = UBound(PhraseRangeArray(0))
Dim SumRng: ReDim SumRng(1 To ElementCount)
i = 1
For Each element In SumRange
SumRng(i) = element
i = i + 1
Next element
Dim SumArray: ReDim SumArray(0 To 2 + UBound(PhraseRangeArray), 1 To ElementCount)
For i = 1 To ElementCount
SumArray(1, i) = SumRng(i)
For RC = 2 To 2 + UBound(PhraseRangeArray)
If InString(CStr(PhraseRangeArray(RC - 2)(i, 1)), CStr(CriteriaArray(RC - 2))) Then
SumArray(RC, i) = 1
Else
SumArray(RC, i) = 0
End If
Next RC
SumArray(0, i) = SumArray(1, i)
For Mult = 2 To 2 + UBound(PhraseRangeArray)
SumArray(0, i) = SumArray(0, i) * SumArray(Mult, i)
Next Mult
Next i
SumIfsContains = 0
For Item = 1 To ElementCount
SumIfsContains = SumIfsContains + CDbl(SumArray(0, Item))
Next Item
End Function
但是我仍然很好奇如何使 Range/Criteria 对不只是稍后从“Criteria”数组中分离出来。