Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Triggering an Event when a Worksheet is Deactivated.

Triggering an Event when a Worksheet is Deactivated

by Allen Wyatt
(last updated March 9, 2013)

4

John needs to ensure that certain actions have taken place (information added, etc.) before a user leaves a worksheet. He wonders if there is there some sort of macro event such as WorksheetBeforeDeactivate.

There are actually two events you could use for this purpose. You can use the SheetDeactivate event in the ThisWorkbook module to trigger actions whenever a user leaves any worksheet in the workbook:

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    MsgBox Prompt:="You just left sheet:" & Sh.Name
End Sub

If you want to trigger actions only when they leave a particular worksheet, then you can use the Deactivate event in the WorkSheet object:

Private Sub Worksheet_Deactivate()
    ' sheet specific code goes here
End Sub

You should know, however, that in either case the worksheet to which the user is choosing to go will be the active worksheet after the event is completed. If you want to force the user to stay on the worksheet, you need to specifically put them back on the worksheet, in this manner:

Private Sub Worksheet_Deactivate()
    ' sheet specific code goes here
    Sheets("Sheet1").Select
End Sub

This assumes, of course, that the name of the worksheet you want the user to remain on is Sheet1.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7728) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Triggering an Event when a Worksheet is Deactivated.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing Print Dialog Box Defaults

Some of the built-in defaults in Word can't be changed. Often times, however, you can work around these defaults by using ...

Discover More

Adding an Image to a Document

They say that a picture is worth a thousand words, so it is fortunate that Docs allows you to easily add images to your ...

Discover More

Mixing Note Numbering

When inserting footnotes and endnotes in a document, most of us don't give much thought to the format used in the numbering ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Retrieving Drive Statistics

Need to gather some information about the drives on a system? It can be pretty easy to do using a macro, as shown in this ...

Discover More

Specifying Location for a Message Box

When writing macros, you may want to position a message box at a specific location on the screen. This can't be done in ...

Discover More

Assigning a Macro to a Keyboard Combination

Macros are a great way to expand what you can do with the data you place in Excel. You can make those macros even handier by ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 4?

2016-03-30 12:20:37

Willy Vanhaelen

@urvill

This will work however and the effect is the same:

Private Sub Worksheet_Activate()
Range("A1").Select
End Sub


2016-03-30 12:05:50

Willy Vanhaelen

@urvil

I don't think you can. When Worksheet_Deactivate runs, the newly selected sheet is already the active one and the sheet you left is not active anymore. You cannot change the cursor position on an inactive sheet.


2016-03-29 11:27:43

urvil

How To select A1 cell before the sheet get deactivate in this.


2016-03-24 08:07:40

Venkat

Thanks a great site to learn Excel VBA !

Regards

Venkat


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.