Results 1 to 4 of 4

Thread: How to close workbook after specified time limit ?

  1. #1
    Join Date
    Apr 2009
    Posts
    3

    How to close workbook after specified time limit ?

    hello,

    In my Microsoft Excel, I want to close the workbook after a specified amount of time. I know this feature is impressive and is possible by using macros but I don't know the macro code. Can anyone provide me the macro code to do the required task ?

    thanks..

  2. #2
    Join Date
    Apr 2008
    Posts
    2,139

    Re: How to close workbook after specified time limit ?

    Here's the macro code for you....

    Code:
    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
    This macro will automatically close the workbook in which it is located after that workbook has been opened for a certain period of time.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,139

    Re: How to close workbook after specified time limit ?

    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.

    • To use this macro, it must be inserted into the "ThisWorkbook" part of the left pane in the VBA editor window. To get to the VBA editor window simply hit "alt + F11" and look to the left pane in that window.

    • 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.

  4. #4
    Join Date
    May 2012
    Posts
    1

    Re: How to close workbook after specified time limit ?

    I have a problem with this macro. If I do everything so the workbook does not close automatically ..... do not know where it could be a problem.
    I attach a photo if everything is OK
    Please help me.....
    Thanks
    Last edited by bhaskar; 04-05-2012 at 05:29 PM. Reason: Attachment removed, please add file in JPG format

Similar Threads

  1. How to remove time limit remover from Windows 8
    By Buzz Barton in forum Operating Systems
    Replies: 3
    Last Post: 30-10-2011, 10:54 PM
  2. Replies: 3
    Last Post: 11-07-2011, 10:37 AM
  3. Replies: 2
    Last Post: 09-07-2009, 08:57 AM
  4. Maximum limit for setting a sleep time
    By Kamran in forum Operating Systems
    Replies: 3
    Last Post: 04-05-2009, 10:59 AM
  5. Windows 7 Time limit
    By Ryder in forum Operating Systems
    Replies: 7
    Last Post: 17-04-2009, 10:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,874,874.90534 seconds with 17 queries