I'm new to VBA and am having trouble with this function. My goal is to find the difference between the sums of the cells in two ranges (sourceDR and sourceCR) which match a value (given in match) in a criterion range (subset).

Public Function trial(sourceDR As Range, sourceCR As Range, subset As Range, match As Range) As Double
Dim dr As Double, cr As Double
dr = WorksheetFunction.SumIfs(sourceDR, subset, match)
cr = WorksheetFunction.SumIfs(sourceCR, subset, match)
If dr > cr Then
trial = dr - cr
Else
trial = 0
End If
End Function
The function is returning #Value! when I attempt to evaluate it in a worksheet. For example, if the data ranges are b1:b4 and c1:c4, the criterion range is a1:a4, and the value to be matched is in a6, I enter
Formula: copy to clipboard
=trial(b1:b4, c1:c4, a1:a4, a6)
into b6 and get #Value!. If I omit the sourceCR argument (c1:c4), the function correctly returns the sum of b1:b4 for which the criterion in a1:a4 matches a6.

Thanks for your help.