I'm trying to code the following in VBA:
=SUMIFS(N2:N29,D2:D29,IF(COUNTIF(D2:D29,A36)=0,"*","="&A36),C2:C29,IF(COUNTIF(C2:C29,B36)=0,"*","="&B36),E2:E29,IF(COUNTIF(E2:E29,C36)=0,"*","="&C36),F2:F29,IF(COUNTIF(F2:F29,D36)=0,"*","="&D36))
The nested If and Countif are there so the Sumif ignores any condition that isn't required by the user (the user inputs A:B36), but still sums cells that meet all the remaining conditions.
I've written the following in VBA (with criterias 1-3 determined in the same way):
If Application.WorksheetFunction.CountIf(Worksheets("Benchmark").Range("V" & i), Box4.Value) = 0 Then
criteria4 = "*"
Else
criteria4 = "=" & Box4.Value
End If
If criteria1 = "*" And criteria2 = "*" And criteria3 = "*" And criteria4 = "*" Then
dummy = 0
Else
dummy = Application.WorksheetFunction.SumIfs(Worksheets("Benchmark").Range("H" & i), Worksheets("Benchmark").Range("S" & i), criteria1, Worksheets("Benchmark") _
.Range("R" & i), criteria2, Worksheets("Benchmark").Range("C" & i), criteria3, Worksheets("Benchmark").Range("V" & i), criteria4)
The sumifs is summing all cells that meet at least one of the conditions supplied, rather than all of them.
To add context, the purpose of this code is to loop through a range, and find cells that meet criteria supplied by the user. However, if the user opts to supply certain criteria but not others, then those criteria not supplied are ignored. I've used a sumifs because my understanding is that the function will sum all cells that meet the set of criteria supplied. I can then say, if sumifs>0 then cell meets criteria supplied by the user.
i? Is it a single number, or part of a range (e.g."3:H10")? - Chronocidal