我试图在VBA中编写CONCATENATEIFS
函数,其作用类似于SUMIFS
等。以下是一个示例调用:
=ConcatenateIfs(",",$E$6:$E$9,$F$6:$F$9,"Something",$G$6:$G$9,">=2")
Public Function ConcatenateIfs(JoinStr As String, StrRange As Range, ParamArray var() As Variant) As String
Dim numberOfConditions As Integer
numberOfConditions = (UBound(var) - LBound(var) + 1) / 2
Dim tmpResult As String
tmpResult = ""
Dim includeItem As Boolean
For Item = 1 To StrRange.Count
includeItem = True
For Condition = 1 To numberOfConditions:
If var((Condition - 1) * 2)(Item) <> var((Condition - 1) * 2 + 1) Then
includeItem = False
End If
Next Condition
If includeItem = True And Item = 1 Then
tmpResult = StrRange(Item)
ElseIf includeItem = True Then
tmpResult = tmpResult + JoinStr + StrRange(Item)
End If
Next Item
ConcatenateIfs = tmpResult
End Function
上述函数似乎永远不会认识到条件已经满足,这意味着代码的这部分不起作用(即它总是将不等式评估为True):
If var((Condition - 1) * 2)(Item) <> var((Condition - 1) * 2 + 1) Then
includeItem = False
End If
如何解决这个问题,以便在criteria_range项和条件本身之间正确测试条件?加分点:如果不符合标准(正确),我该如何摆脱条件循环?