I don't know how to create a function for your situation but I can try another way
Try this code
Sub SumBasedOnCustodian()
Dim LR As Long
Dim I As Long
LR = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Range("J2:J" & LR).ClearContents
For I = 2 To LR
If Not IsEmpty(Cells(I, 10).Offset(, -3)) And Cells(I, 10).Offset(-1, -3) <> Cells(I, 10).Offset(, -3) Then
If Cells(I, 10).Offset(, -3) <> Cells(I, 10).Offset(1, -3) Then
Cells(I, 10).Value = Cells(I, 10).Offset(, -1).Value
ElseIf Cells(I, 10).Offset(, -3) = Cells(I, 10).Offset(1, -3) And Cells(I, 10).Offset(1, -3) <> Cells(I, 10).Offset(2, -3) Then
Cells(I, 10).Value = "OK"
Cells(I, 10).Value = Cells(I, 10).Offset(, -1).Value + Cells(I, 10).Offset(1, -1)
Else
Cells(I, 10).FormulaArray = "=SUM(OFFSET(RC[-1],0,0,MATCH(1,IF(RC[-1]:R[37]C[-1]="""",1,0),0),1))"
Cells(I, 10).Value = Cells(I, 10).Value
End If
End If
Next I
Application.ScreenUpdating = True
End Sub