Help with code forcing specific workbook to close after inactivity.

Sean15

Well-known Member
Joined
Jun 25, 2005
Messages
719
Office Version
  1. 2010
Platform
  1. Windows
Hi:

This code closes Excel workbook after two minutes of inactivity. But I noticed something odd. If I have two workbooks open, Excel closes the last open workbook when the code executes. Could code be adjusted so Excel will only close workbook SW 070222.xlsm after two minutes of inactivity? Code saved in VBA project module 1

File path: C:\Users\mycar34\Documents\Articles\SW 070222.xlsm

VBA Code:
Dim CloseTime As Date

Sub TimeSetting()

Application.CutCopyMode = False

CloseTime = Now + TimeValue("00:02:00")

On Error Resume Next

Application.OnTime EarliestTime:=CloseTime, _

Procedure:="SavedAndClose", Schedule:=True

End Sub

Sub TimeStop()

On Error Resume Next

Application.OnTime EarliestTime:=CloseTime, _

Procedure:="SavedAndClose", Schedule:=False

End Sub

Sub SavedAndClose()

ActiveWorkbook.Close Savechanges:=True

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I'm just guessing here,
but perhaps instead of

ActiveWorkbook.Close Savechanges:=True
you specify the workbook of interest, which presumably is the one where this code is, so
ThisWorkbook.Close Savechanges:=True

It might also help if you stick a WorkbookActivate and WorkbookDeactivate procedure in the workbook module to cause the workbook to keep monitoring the passage of time, in case you have conflicting code somewhere else in the project that is interrupting the procedure you posted.

Did I mention I'm just guessing here. :)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top