I wrote user-defined function in Excel, but it seems to slow down work significantly. Function performs sumifs for several worksheets in workbook.
Could you advise me, please, on potential optimization of it?
Main idea of the function - to create sumifs, which would sum data on the same criterias from different worksheets.
Arguments of function:
wblist - Range with names of worksheets, where sumifs should be performed
SumRng - Range of cells to be summed on each worksheet
Optional CritRangeN - Range of cells to be assessed with criteria (CritN)
How it works 1) Understands, how many criterias were passed to the function 2) Sums sumifs (with already defined number of arguments) for worksheets, which were mentioned in the wblist Range
Code is presented below
Public Function SUMIFS3D(wblist As Range, SumRng As Range, _
Optional CritRange1 As Variant, Optional Crit1 As Variant, _
Optional CritRange2 As Variant, Optional Crit2 As Variant, _
Optional CritRange3 As Variant, Optional Crit3 As Variant, _
Optional CritRange4 As Variant, Optional Crit4 As Variant, _
Optional CritRange5 As Variant, Optional Crit5 As Variant, _
Optional CritRange6 As Variant, Optional Crit6 As Variant, _
Optional CritRange7 As Variant, Optional Crit7 As Variant)
Dim cell As Range
Dim wkb As Workbook
Set wkb = Application.Caller.Parent.Parent
Dim paramN As Integer
'Application.Volatile
paramN = 0
If Not IsMissing(CritRange1) Then
paramN = paramN + 1
End If
If Not IsMissing(CritRange2) Then
paramN = paramN + 1
End If
If Not IsMissing(CritRange3) Then
paramN = paramN + 1
End If
If Not IsMissing(CritRange4) Then
paramN = paramN + 1
End If
If Not IsMissing(CritRange5) Then
paramN = paramN + 1
End If
If Not IsMissing(CritRange6) Then
paramN = paramN + 1
End If
If Not IsMissing(CritRange7) Then
paramN = paramN + 1
End If
Select Case paramN
Case 1
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1)
Next cell
Case 2
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1, _
wkb.Sheets(cell.Value).Range(CritRange2.Address), Crit2)
Next cell
Case 3
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1, _
wkb.Sheets(cell.Value).Range(CritRange2.Address), Crit2, _
wkb.Sheets(cell.Value).Range(CritRange3.Address), Crit3)
Next cell
Case 4
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1, _
wkb.Sheets(cell.Value).Range(CritRange2.Address), Crit2, _
wkb.Sheets(cell.Value).Range(CritRange3.Address), Crit3, _
wkb.Sheets(cell.Value).Range(CritRange4.Address), Crit4)
Next cell
Case 5
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1, _
wkb.Sheets(cell.Value).Range(CritRange2.Address), Crit2, _
wkb.Sheets(cell.Value).Range(CritRange3.Address), Crit3, _
wkb.Sheets(cell.Value).Range(CritRange4.Address), Crit4, _
wkb.Sheets(cell.Value).Range(CritRange5.Address), Crit5)
Next cell
Case 6
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1, _
wkb.Sheets(cell.Value).Range(CritRange2.Address), Crit2, _
wkb.Sheets(cell.Value).Range(CritRange3.Address), Crit3, _
wkb.Sheets(cell.Value).Range(CritRange4.Address), Crit4, _
wkb.Sheets(cell.Value).Range(CritRange5.Address), Crit5, _
wkb.Sheets(cell.Value).Range(CritRange6.Address), Crit6)
Next cell
Case 7
For Each cell In wblist
SUMIFS3D = SUMIFS3D + _
WorksheetFunction.SumIfs(wkb.Sheets(cell.Value).Range(SumRng.Address), _
wkb.Sheets(cell.Value).Range(CritRange1.Address), Crit1, _
wkb.Sheets(cell.Value).Range(CritRange2.Address), Crit2, _
wkb.Sheets(cell.Value).Range(CritRange3.Address), Crit3, _
wkb.Sheets(cell.Value).Range(CritRange4.Address), Crit4, _
wkb.Sheets(cell.Value).Range(CritRange5.Address), Crit5, _
wkb.Sheets(cell.Value).Range(CritRange6.Address), Crit6, _
wkb.Sheets(cell.Value).Range(CritRange7.Address), Crit7)
Next cell
End Select
End Function