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: Forcing a Workbook to Close after Inactivity.

Forcing a Workbook to Close after Inactivity

Written by Allen Wyatt (last updated March 30, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003


Dave wonders if he can force a workbook to close after a certain amount of time, provided it is not currently being used. In his office people open workbooks that are on the server and then forget that they are open. When that occurs, nobody else can edit them, so he would like to force workbooks to close if left unattended for 60 minutes.

It is possible to do this using macros, but you may not really want to do that from a business or user-oriented perspective. For instance, let's say that a user has three workbooks open on his system, so that comparisons can be made between them. It is possible to get "tied up" with two of the workbooks for quite a while, with the third one being the one that triggers a shutdown. Excel's VBA isn't terribly discriminating—when a workbook is closed, it is typically the one which has focus at the current time.

Further, what do you do with unsaved changes when closing? If you save them, you run into the issue that perhaps the user didn't intend to save them. If you don't save them, the converse problem occurs—perhaps there was a lot of data that needed to be saved. You can't have the closing procedure ask if information should be saved; that would keep the workbook tied up as surely as keeping it open (and unused) would.

A possible solution is to simply share the workbook. If you enable sharing (as discussed in other ExcelTips), then multiple people can have the same workbook open at the same time. If one of those people leaves it open, then nobody else is inconvenienced because they can still open it and, optionally, make changes in the workbook.

If you decide to go the macro-based route, then the solution is rather simple. You need some sort of timer structure (easily implemented through use of the OnTime method) and some way to check to see if someone is doing something in the workbook.

To start, add the following code to a standard macro module. Note that there are three routines to be added:

Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("01:00:00")
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=True
End Sub
Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=False
 End Sub
Sub ShutDown()
    Application.DisplayAlerts = False
    With ThisWorkbook
        .Saved = True
        .Close
    End With
End Sub

These three routines are fairly straightforward. The first two respectively turn on the timer and turn it off. Note that these routines utilize the DownTime variable, which is declared outside of any of the routines. In this way its contents can be utilized in multiple routines.

The third routine, ShutDown, is the one that actually closes the workbook. It is only invoked if the OnTime method expires, at the end of an hour. It closes the workbook without saving any changes that may have been made.

The next routines (there are four of them) need to be added to the ThisWorkbook object. Open the VBA Editor and double-click on the ThisWorkbook object in the Project Explorer. In the code window that Excel opens, place these routines:

Private Sub Workbook_Open()
    Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call StopTimer
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call StopTimer
    Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Excel.Range)
    Call StopTimer
    Call SetTimer
End Sub

The first two routines are triggered when the workbook is opened and when it is closed; they start the timer and turn it off. The other two routines are executed automatically whenever a worksheet is recalculated or whenever someone makes a selection in the workbook. Both are good indicators that someone is using the workbook (it is not inactively open). They stop the timer and then restart it, so that the one-hour countdown starts over.

There is a downside to using a set of macros such as these: you effectively eliminate Excel's Undo capability. When a macro is executed, the Undo stack is automatically wiped out by Excel. Since macros are running with every change made in the workbook, the person's changes cannot be undone. (There is no way to get around this drawback.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2281) 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: Forcing a Workbook to Close after Inactivity.

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

Understanding Hard and Soft Returns

Did you know that there are different types of returns in Word? Here's the inside scoop.

Discover More

Searching for Breaks

Word allows you to insert different types of breaks in your text that help control how your document is paginated. If you ...

Discover More

Searching for Formatting

When searching for text, Word can pay attention to more than just the characters in the text. It can also pay attention ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Turning Off Sharing

All good things must come to an end at some point. When you are done sharing your workbook with others, this is how you ...

Discover More

Opening a Workbook as Read-Only

When you need to work on a workbook, you may want to do so without modifying the original contents of the workbook. This ...

Discover More

Accessing a Problem Shared Workbook

What are you to do is you share a workbook with others, and then suddenly the workbook won't open properly? Dealing with ...

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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 two less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.