Close a Workbook after a Time Limit is Reached

Add to Favorites
This macro will automatically close the workbook in which it is located after that workbook has been opened for a certain period of time. This is a great feature when you are sharing workbooks that can't be open at the same time on more than one computer; in that situation this macro ensures that if someone forgets to close the file, it will automatically close anyway and give others the ability to then work on the file. This is also something which can be used to let you know how many minutes you have left in a certain session, as defined by the total amount of time you should have in the session. Basically, it is a cool way to have a little more control over workbooks which you may send out to other people or use yourself.

Note: Macros must be enabled on the user's computer for this macro to take affect.

Note: To change the amount of time this excel file is allowed to be open, change the "180" in this line of code: "TimeInMinutes = 180" to any number of minutes that is needed. You can also change the messages that appear in the following two lines of code by simply changing the text only between the quotation marks.

MsgBox "This file has been open for " & TotalTime / 60 & " minutes. You have 5 minutes to save before Excel closes."

MsgBox "Excel will now close."

Where to install the macro:  ThisWorkbook

Close a Workbook after a Time Limit is Reached

Private Sub Workbook_Open()

Dim Start, Finish, TotalTime, TotalTimeInMinutes, TimeInMinutes
Application.DisplayAlerts = True
TimeInMinutes = 180 'Timer is set for 180 minutes; change as needed.
If TimeInMinutes > 5 Then
TotalTimeInMinutes = (TimeInMinutes * 60) - (5 * 60)
   Start = Timer
   Do While Timer < Start + TotalTimeInMinutes
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start
   Application.DisplayAlerts = False
   MsgBox "This file has been open for " & TotalTime / 60 & " minutes.  You have 5 minutes to save before Excel closes."
End If

Start = Timer
   Do While Timer < Start + (5 * 60)
       DoEvents
   Loop
   Finish = Timer
   TotalTime = Finish - Start    
   Application.DisplayAlerts = False
   MsgBox "Excel will now close."
   Application.Quit
End Sub







Similar Content on TeachExcel
Prevent Saving a Workbook under a Different File Name
Macro: This Excel macro prevents the user from saving an excel file or workbook under a diffe...
Run a Macro when a User Does Something in the Workbook in Excel
Tutorial: How to run a macro when a user does something within the Workbook in Excel, such as openi...
VBA IF Statement in Excel Macros
Tutorial: IF statements allow you to make decisions in your Macros and VBA in Excel. An IF statement...
Automatically Lock Certain Cells in Excel using a Macro
Tutorial: This macro allows you to have a cell automatically locked after a user enters something in...
Automatically Run a Macro at a Certain Time - i.e. Run a Macro at 4:30PM every day
Macro: Automatically run an Excel macro at a certain time. This allows you to not have to wo...
Run a Macro when a User Does Something in the Worksheet in Excel
Tutorial: How to run a macro when a user does something in the worksheet, everything from selecting ...



How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.


Similar Content
Prevent Saving a Workbook under a Different File Name
Macro: This Excel macro prevents the user from saving an excel file or workbook under a diffe...
Run a Macro when a User Does Something in the Workbook in Excel
Tutorial: How to run a macro when a user does something within the Workbook in Excel, such as openi...
VBA IF Statement in Excel Macros
Tutorial: IF statements allow you to make decisions in your Macros and VBA in Excel. An IF statement...
Excel Forum