Can't activate sheet in function

lynxbci

Board Regular
Joined
Sep 22, 2004
Messages
201
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,

I dont think that my question was clear enough in the earlier post. The function below worked on a single sheet, but then i added code to enable it to look at other worksheets in the workbook, and it stopped working.

The issue i think is with the line below, as it is not making nsht active!!
Code:
Sheets(nsht).Activate
R = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Row - 1

This is the full function
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

' **** Added parts ****

Dim osht As String
Dim nsht As String

osht = ActiveSheet.Name
nsht = rng1.Worksheet.Name

Sheets(nsht).Activate

'***** end of added parts *******
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

' ****** added part *****
Sheets(osht).Activate
' ***** end of added part ****

End Function

Any suggestions please?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Is this a function that is referenced in a worksheet cell?

And why are you trying to activate sheets instead of referencing them directly?
 
Upvote 0
Hi,

How do i reference them dircetly?

thanks
 
Upvote 0
Remove your newly added parts, and change your R calculation to be:
Code:
R = rng1.Worksheet.UsedRange.Rows.Count + rng1.Worksheet.UsedRange.Row - 1
... at a guess.
 
Upvote 0
Somtimes i think i should just stay in bed on Fridays........

Thanks for the help Glenn
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,287
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