TeachExcel.com
Video Tutorials, Macros, Shortcuts, and More...
TE
Home Online Excel Classes Downloadable Tutorials & Guides Consulting Services Excel Help Now Contact Us
Excel Tips Excel Blog Excel Video Tutorials Excel Macros Forum
Video Tutorials
  • Free Macros
  • Excel 2007 Resources
  • Keyboard Shortcuts
  • Forum
  • Contact/About
  • New Excel Interface & Features
  • Getting Started with Excel 2007
  • Intro. to Formulas and Functions
  • Intermediate Formulas/Functions
  • Printing - Formatting - Saving
  • Sorting, Filtering, & Tables
  • Data Consolidation & Retrieval
  • Security in Excel 2007
Macros
Excel Tutorials For Macros

Close a Workbook after a Time Limit is Reached

Bookmark and Share

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


Bookmark and Share


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.

      For Excel Versions Prior to Excel 2007
      Go to Tools > Macros > Visual Basic Editor

      For Excel 2007
      Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic

  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.

    1. Go to the menu at the top of the window and click Insert > Module
    2. Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
    3. Go to Step 8.

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

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Go to Step 8.

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

    1. Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
    2. Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
    3. A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
    4. Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
    5. Go 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.

Random Tutorials
Goal Seek Feature in Excel
(Intermediate)
Create a Two-Way Lookup in Excel
(Intermediate)
Extract Text from Cells - Intermediate Example
(Intermediate)
Remove #N/A Error Result from Empty VLOOKUP() Formulas
(Intermediate)
Lookups With MATCH() and INDEX() Functions
(Intermediate)
HLOOKUP() Function - Introduction
(Intermediate)
Submit Inquiry Here
  • Prices From $10
Name:*
E-mail:*
Request:*
The macro(s) on this page will be sent with the request.
Contact | Privacy Policy | Disclaimer
Copyright© 2009 TeachExcel.com