Run Excel VBA When Leaving a Worksheet for a Specific Sheet
Question details
The user wants to automatically run a VBA macro when navigating away from a specific worksheet, but needs to prevent the code from executing if the destination is one particular excluded sheet.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating tasks via worksheet events, specifically triggering an action upon leaving a sheet unless heading to a designated exclusion sheet.
- Observed behavior
- Requires a script utilizing the Worksheet_Deactivate event combined with a condition checking the active destination sheet.
Ensure that the Developer tab is enabled in your ribbon and that macros are permitted to run in your Trust Center settings.
Use the Worksheet_Deactivate Event
Implement the Worksheet_Deactivate event in the specific sheet's VBA module to run code when leaving, while checking the active sheet's name to exclude specific destinations.
The Worksheet_Deactivate event triggers immediately after you leave a worksheet. At the moment this event fires, the destination sheet has already become the 'ActiveSheet'. By checking the name of the new ActiveSheet, you can instruct the macro to exit without running the rest of your code.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.
In the Project Explorer pane on the left, locate your workbook and double-click the sheet you are leaving (e.g., 'Sheet2'). This opens the code module specific to that worksheet.
Paste the following code into the code window: Private Sub Worksheet_Deactivate() If ActiveSheet.Name = "Sheet1" Then Exit Sub Else ' Add your code here MsgBox "Code is running because you are leaving Sheet2 for another sheet." End If End Sub
Replace 'Sheet1' with the exact name of the sheet you want to exclude. Replace the MsgBox line with your actual macro code.

Automate Your Worksheets Seamlessly with WPS Office
WPS Spreadsheet offers robust support for VBA macros, allowing you to use Worksheet_Deactivate and other event handlers exactly as you would in Microsoft Excel, ensuring your automated workflows transition perfectly.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your workbook in WPS Spreadsheet.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click the 'VBA Editor' icon.
- 3. Apply Your Macro: In the Project Explorer, double-click the specific worksheet module (e.g., Sheet2), paste your Worksheet_Deactivate code, and save your document.

Frequently Asked Questions
Why isn't my Worksheet_Deactivate macro running?
Ensure that macros are enabled in your application settings. Additionally, check if events have been disabled in VBA. You can re-enable them by running 'Application.EnableEvents = True' in the Immediate Window.
Can I run a macro only when navigating to a specific sheet instead of leaving one?
Yes. Instead of using Worksheet_Deactivate on the sheet you are leaving, you can use the Worksheet_Activate event inside the VBA module of your specific destination sheet.
How do I exclude multiple sheets from triggering the deactivate macro?
You can use the logical 'Or' operator in your If statement (e.g., If ActiveSheet.Name = "Sheet1" Or ActiveSheet.Name = "Sheet3" Then Exit Sub) or utilize a Select Case statement to list all excluded sheet names easily.




