bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Macros > VBA Examples > Automatically Closing a Workbook

Automatically Closing a Workbook

Summary: Walk away from your computer, and your work is visible on the screen for all to see. For security purposes you may want a workbook to close automatically if it isn’t used within a certain period of time. This tip discusses how you can create macros to accomplish that task. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)

If you work in a security-conscious environment, you may always be looking for ways to increase the security of your system, even without any effort on your part. One way you can increase security is to add some macro coding to your workbook that results in it being closed if you don't make any changes to a worksheet within any ten-minute period. For instance, if you step a way from your computer, then the workbook you were using is automatically closed after 10 minutes.

The following series of three macros will implement just such a system. The first macro (Workbook_Open) is executed when a workbook is first opened. It uses the run_time subroutine, which uses the OnTime method to specify that ten minutes in the future the close_wb macro will be run.

The second macro (Worksheet_Change) is triggered whenever you change something in a workbook. When this occurs, the same run_time subroutine is used to again specify that close_wb should be run ten minutes in the future.

Private Sub Workbook_Open()
    close_time = Now + TimeValue("00:10:00")
    run_time
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If close_time Then
        Application.OnTime _
          EarliestTime:=close_time, _
          Procedure:="close_wb", _
          Schedule:=False
        close_time = Empty
    End If
    close_time = Now + TimeValue("00:10:00")
    run_time
End Sub
Public close_time
Sub run_time()
    Application.OnTime _
      EarliestTime:=close_time, _
      Procedure:="close_wb", _
      Schedule:=True
End Sub
Sub close_wb()
    Application.DisplayAlerts = False
    With ThisWorkbook
        .Saved = True
        .Close
    End With
End Sub

If the close_wb macro is ever executed, any changes to the current workbook are discarded, and the workbook is closed.

Tip #2281 applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003


Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
 
Check out ExcelTips Premium today!

Helpful Links

Ask an Excel Question
Make a Comment

Tips.Net Home

ExcelTips FAQ
ExcelTips Premium

Learn Access Now

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

 

Great Info!

Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)