lynxbci
Board Regular
- Joined
- Sep 22, 2004
- Messages
- 201
- Office Version
- 365
- Platform
- Windows
- MacOS
I have written a little function to perform a sumif with 2 criteria inputs and it works perfectly, until i try and use it on a different sheet to the one where the result is showing up......what have i done wrong please?
Code:
Function Sum2if(rng1 As Range, crt1 As Variant, rng2 As Range, crt2 As Variant, tots As Range)
Dim R As Integer
Dim i As Integer
Dim osht As String
Dim nsht As String
osht = ActiveSheet.Name
nsht = rng1.Worksheet.Name
Sheets(nsht).Activate
R = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1
For i = 1 To R
If rng1.Cells(i, 1) = crt1 And rng2.Cells(i, 1) = crt2 Then
Sum2if = Sum2if + tots.Cells(i, 1)
End If
Next i
Sheets(osht).Activate
End Function