Function won't activate other worksheet

lynxbci

Board Regular
Joined
Sep 22, 2004
Messages
201
Office Version
  1. 365
Platform
  1. Windows
  2. 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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top