Worksheet Event Deactivate

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
I noticed a funny thing about the Deactivate-Event, and I was just curious whether someone else has noted the same, or if I am simply doing anything wrong.

I have this code:

Private Sub Worksheet_Deactivate()
Dim CurrentSheet As String
CurrentSheet = ActiveSheet.Name
MsgBox "You are deactivating worksheet " & CurrentSheet
End Sub

I have attached this code to Sheet1. When I leave Sheet1 to go to Sheet2, the code runs, but the value of CurrentSheet is not Sheet1 but Sheet2. It seems to me then, that this code is executed when Sheet2 is activated, rather than when Sheet1 is deactivated.

Another observation is its cousin, the Activate-Event.

I have this code:

Private Sub Worksheet_Activate()
CurrentSheet=ActiveSheet.Name
MsgBox "Welcome to " & CurrentSheet
End Sub

If Sheet2 is active and I click on Sheet1, The value of CurrentSheet Sheet1. This leads me to believe that the Activate-event and the DeActivate-event is really the same thing: Deactivate is triggered when the next sheet becomes active, as does Activate.

Am I wrong here.

Bengt

Running Excel2003 under W7.
 
If you work from Workbook_SheetDeActivate event in ThisWorkbook, you get the Sh argument.
Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)

    MsgBox "from " & Sh.Name & " To " & ActiveSheet.Name
    
    If Sh.Name = "KeyWorksheet" Then
        Rem do something
    End If
End Sub
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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