Microsoft Office Excel Macro
Close a Workbook after a Time Limit is Reached
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."
Macro Installation Location: ThisWorkbook
Keywords: sort data sorting excel one column next subset adjacent data set table advanced
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