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. If your VBA code isn't written correctly, it may end up shutting down whichever workbook has focus at the current time—clearly a result you don't want to occur.
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 or co-author (Excel 2019 and Excel in Office 365) the workbook. If you enable sharing or co-authoring (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.)
Another problem is that macros can only be stored in macro-enabled workbooks. Thus, the macro-based solution will not work for regular XLSX files, as they don't allow macros within them. In that case you are limited to non-macro solutions, such as turning on workbook sharing or co-authoring.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8192) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Forcing a Workbook to Close after Inactivity.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Need to allow others to contribute to your Excel workbook? It's easy to do if you just share it. This tip provides an ...
Discover MoreAll good things must come to an end at some point. When you are done sharing your workbook with others, this is how you ...
Discover MoreWhen you need to work on a workbook, you may want to do so without modifying the original contents of the workbook. This ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-11-14 05:06:45
Hans Hallebeek
HI, I would call the StopTimer the moment you invoke the SetTimer
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments