workbook close on timer - query

orsm6

Well-known Member
Joined
Oct 3, 2012
Messages
511
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I am using this code in a workbook to activate a timer when it is opened, then after 15 minutes make it close and save changes. my question is, if I have this workbook open, then open another the timer seems to activate on that one too, then as a iswitch between it starts the timer again.

I have no issue with timer restarting, but how can I stop the macro from starting the timer on any other work book?

Code:
Private Sub Workbook_Activate()
    On Error Resume Next
    Application.OnTime dTime + TimeValue("00:15:00"), "CloseMe", , False
    On Error GoTo 0
    
End Sub
 
Private Sub Workbook_Deactivate()
    dTime = Time
    Application.OnTime dTime + TimeValue("00:15:00"), "CloseMe"
End Sub

TIA :)
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
also, what if the workbook that I want to close is in the background? would this code shut the workbook that is currently up on the screen.

cheers.
 
Upvote 0
hi all... got this working.

for those that are interested, here is the code

pasted into ThisWorkbook
Code:
Private Sub Workbook_Open()
    
    
    dTime = Time
    On Error Resume Next
    Application.OnTime dTime + TimeValue("00:15:00"), "CloseMe"
    On Error GoTo 0
    
End Sub
 
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    Application.OnTime dTime + TimeValue("00:15:00"), "CloseMe", , False
    dTime = Time
    Application.OnTime dTime + TimeValue("00:15:00"), "CloseMe"
    On Error GoTo 0
End Sub

pasted into a new module
Code:
Public dTime As Date
Sub CloseMe()
'macro will close this workbook and save the changes made

    ThisWorkbook.Close Saved = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
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