Share via

When deactivating a worksheet how do I run a code if going to 'Sheet 3' but not 'sheet 1'?

Anonymous
2023-07-28T08:45:46+00:00

Hi

When I use 'Private Sub Worksheet_Deactivate()' on 'sheet 2', is there a way to code will run if i click on 'sheet 3' but will not run if i'm going to 'sheet 1'?

I've tried using

Private Sub Worksheet_Deactivate()

If Active.Name = "sheet 1" Then

exit sub

else

'run code

end if

end sub

and

Private Sub Worksheet_Deactivate()

If Worksheets("Sheet 1").Active = True Then

exit sub

else

'run code

end if

end sub

but these do not work, just come up with 'run-time error '438': Object doesn't support the property or method' and highlight the 'if.....then' parts.

than you,

Dan

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-07-28T09:53:00+00:00

Hi Daniel K, I'm Femi and I'd be happy to help you with your question. I understand that you are trying to make a code run when you deactivate 'Sheet 2' to go to 'Sheet 3', but not when you go to 'Sheet 1'. To do this, kindly make use of the code below: Private Sub Worksheet_Deactivate() If ActiveSheet.Name = "Sheet1" Then Exit Sub Else ' Your code here MsgBox "Code is running because you are deactivating Sheet2 to go to another sheet." End If End Sub

Kindly replace 'Your code here' with your actual code. This way, the code will run when you go from 'Sheet 2' to any other sheet except 'Sheet 1'.

Kindly let me know if this helps!

Best Regards, Femi

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-07-28T10:17:48+00:00

    Hi Femi,

    This worked,

    Thank you,

    Dan

    Was this answer helpful?

    0 comments No comments