Excel udf: Sumif across multiple sheets
This post describes a basic user defined function that searches multiple ranges and adds corresponding values.
You can also use multiple search values.
VBA code:
Function SumifAMS(lookup_value As Range, ParamArray cellranges() As Variant)
Dim i As Integer, rng1 As Variant, temp As Single, a As Boolean
Dim rng2 As Variant, value As Variant, j As Single
If (UBound(cellranges) + 1) Mod 2 <> 0 Then
MsgBox "The number of range arguments must be even. 2, 4 , 8 ... and so on"
Exit Function
End If
For i = LBound(cellranges) To UBound(cellranges) Step 2
If cellranges(i).Rows.Count <> cellranges(i + 1).Rows.Count Then
MsgBox "The number of rows in range arguments don´t match."
End If
If cellranges(i).Columns.Count <> 1 Then
MsgBox "Range arguments can only have size one column each."
Exit Function
End If
rng1 = cellranges(i).value
rng2 = cellranges(i + 1).value
For j = LBound(rng1) To UBound(rng1)
For Each value In lookup_value
If UCase(rng1(j, 1)) = UCase(value) Then a = True
Next value
If a = True Then temp = temp + rng2(j, 1)
a = False
Next j
Next i
SumifAMS = temp
End Function
Where to copy vba code?
- Press Alt-F11 to open visual basic editor
- Click Module on the Insert menu
- Copy and paste vba code.
- Exit visual basic editor
How to use custom function
Download excel file
Sumif across multiple sheets.xls
Excel 97-2003 *.xls
Related posts:
Excel udf: Filter values existing only in one out of two ranges
Excel udf: Looking up data in multiple cross reference tables
Excel udf: Filter common values between two cell ranges in excel
Excel udf: Combine cell ranges into a single range while eliminating blanks




















This was a very interesting technique. I've been trying to use something similar. Thanks for sharing.