VBA & Macro Problems

Run Excel VBA When Leaving a Worksheet for a Specific Sheet

Algirdas JasaitisAlgirdas Jasaitis Oct 12, 2026 872 views

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.

How to Run Excel VBA When Leaving a Specific Worksheet
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.
Before you start

Ensure that the Developer tab is enabled in your ribbon and that macros are permitted to run in your Trust Center settings.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.

2
Access the Source Sheet Module

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.

3
Insert the Deactivate Event Code

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

4
Customize the Script

Replace 'Sheet1' with the exact name of the sheet you want to exclude. Replace the MsgBox line with your actual macro code.

Use the Worksheet_Deactivate Event
Save as Macro-Enabled: Remember to save your file as an Excel Macro-Enabled Workbook (.xlsm) so your VBA code is preserved.

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. 1. Open WPS Spreadsheet: Launch WPS Office and open your workbook in WPS Spreadsheet.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click the 'VBA Editor' icon.
  3. 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.
Fully compatible with Microsoft Excel VBA scripts and macrosSupports native worksheet events including Activate and DeactivateFamiliar Developer tab and comprehensive Visual Basic EditorLightweight, fast, and free to use for your daily tasks
microsoft office alternative - wps office

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.