Excel VBA Worksheet Deactivate: How to Use It
In this article
The Worksheet_Deactivate event in Excel VBA runs automatically when the user leaves a worksheet and moves to another sheet, a chart sheet, or a different workbook. To use it, open the Visual Basic Editor (Alt+F11), double-click the sheet's code module in the Project Explorer, choose Worksheet from the left drop-down, choose Deactivate from the right drop-down, and place your code inside the generated procedure. This is useful for saving state, clearing temporary selections, or updating a summary when a sheet loses focus. Note that exact menu labels can differ slightly by Excel version, device, and region.
What You'll Need
- Excel desktop for Windows or Mac (the VBA editor is not available in Excel for the web).
- A workbook saved as .xlsm or .xlsb, because .xlsx files cannot store macros.
- Macros enabled for the workbook; if you see a security warning, use the Enable Content button only if you trust the file.
- Basic familiarity with the Visual Basic Editor (VBE) and the Project Explorer.
Step-by-Step
- Save your workbook as an Excel Macro-Enabled Workbook (.xlsm). If you skip this, any code you write will be lost when you close the file.
- Press Alt+F11 (Windows) or Fn+Alt+F11 (some Mac keyboards) to open the Visual Basic Editor. You can also go to the Developer tab and click Visual Basic. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list.
- In the Project Explorer (usually on the left; if it is hidden, press Ctrl+R), find your workbook and expand the Microsoft Excel Objects folder.
- Double-click the sheet you want to monitor. The sheet's code module opens in the main code window. Do not use a standard module (Insert > Module) for this event; it must be in the sheet module.
- At the top of the code window, use the left drop-down to select Worksheet. The right drop-down should now list the sheet's events.
- Select Deactivate from the right drop-down. The VBE inserts an empty procedure named Private Sub Worksheet_Deactivate().
- Type your code between the Sub and End Sub lines. For example, to record the time the sheet was left, use: Range("A1").Value = Now. To clear a temporary range, use: Range("B2:B10").ClearContents.
- Press F5 to run the procedure once for testing, or switch back to Excel (Alt+F11 again) and click another sheet to trigger the event naturally.
- Save the workbook with Ctrl+S. Close the VBE and test by moving between sheets several times.
If you need the same behavior on many sheets, you can either copy the procedure into each sheet module or use the Workbook_SheetDeactivate event in the ThisWorkbook module. The workbook-level event fires for every sheet and receives a Sh parameter that identifies which sheet was left, so you can write one procedure instead of many.
Common Mistakes
- Putting the code in a standard module. Worksheet events only fire from the sheet's own code module. Code in Module1 will never run on deactivate.
- Misspelling the procedure name. If you type Worksheet_Deactvate or change the name, Excel will not connect it to the event. Let the VBE generate the stub instead of typing it.
- Using Activate instead of Deactivate. Activate runs when the sheet is entered; Deactivate runs when it is left. Mixing them up produces code that runs at the wrong moment.
- Forgetting to save as .xlsm. A plain .xlsx file silently drops the macro when saved.
- Assuming Deactivate fires when the workbook closes. It does not reliably fire on close. Use Workbook_BeforeClose for cleanup work.
- Relying on Deactivate for critical data changes. If Excel crashes or the user force-quits, the event may not run. Use it for convenience, not for data integrity.
Tips
- Keep Deactivate code short and fast. Long loops or screen updates slow down every sheet switch and make Excel feel sluggish.
- Disable events temporarily with Application.EnableEvents = False before code that switches sheets, then set it back to True. This prevents recursive or unexpected event firing.
- Use Me to refer to the sheet that raised the event, for example Me.Range("A1").Value = Now. It keeps the code readable and avoids hard-coded sheet names.
- For workbook-wide behavior, use the Workbook_SheetDeactivate event in ThisWorkbook. It fires for all sheets and gives you the Sh argument.
- Add error handling (On Error GoTo) so a failure in your Deactivate code does not leave events disabled or the workbook in an odd state.
- Test with events enabled and disabled to confirm your code behaves when other macros temporarily turn events off.
Troubleshooting
If the code does not run, first confirm the procedure sits in the correct sheet module and is named exactly Worksheet_Deactivate. Check that events are enabled: in the VBE's Immediate Window (Ctrl+G), type ?Application.EnableEvents and press Enter. If it returns False, type Application.EnableEvents = True and press Enter. If macros are disabled for the workbook, close it and reopen with Enable Content, or check Trust Center settings under File > Options > Trust Center > Trust Center Settings > Macro Settings. If the code runs but produces wrong results, step through it with F8 to see which line fails. If Excel becomes slow when switching sheets, review the Deactivate code for heavy loops, unnecessary Select or Activate calls, or screen updates; simplify it. If the event fires unexpectedly in another workbook, remember that events are application-wide: code in one open workbook can affect events in another. Close other workbooks with event code while testing. Finally, if nothing works, rebuild the procedure by deleting it and regenerating it from the drop-downs, which rules out hidden characters or a broken module.
Frequently Asked Questions
Does Worksheet_Deactivate fire when I close the workbook?
Not reliably. Use Workbook_BeforeClose for cleanup that must happen on close.
Can I use Worksheet_Deactivate in Excel for the web?
No. VBA macros are not supported in Excel for the web; use the desktop app.
Why does my Deactivate code not run when I switch to another workbook?
Switching to a different workbook does trigger Deactivate for the active sheet, but if events are disabled or the code is in the wrong module it will not run. Check both.
Can I run the same code for every sheet?
Yes. Put a Workbook_SheetDeactivate procedure in the ThisWorkbook module and use the Sh argument to identify the sheet.
Does Deactivate run before or after the new sheet activates?
The old sheet's Deactivate runs before the new sheet's Activate, so code in Deactivate sees the workbook state before the switch completes.
What happens if my Deactivate code shows a message box?
The box appears as the user leaves the sheet, which can be disruptive. Keep Deactivate code silent or log to a cell instead.