I have a macro that starts a timer when the workbook is first opened. When the timer hits "0", the workbook saves and closes. Works fine. I also have it where when the workbook is "Read-Only", the timer does not trigger the "save and close" of the macro. If I have another instance of excel open, and close the macro enabled WB, the macro still runs, even though the WB I have open now has no macros in it.
It will still run the "save and close" function of the original WB macro.
Is there anything I can add to it to make it specific to that WB with the timer macro? Even when it's closed without saving, it still runs the save if there is another excel sheet open.
Any help would be greatly appreciated!
Thank you in advance!
Dim ResetTime As Date
Sub StartTimer()
ResetTime = Now + TimeValue("00:00:30") ' Set initial timer for 2 minutes
Application.OnTime ResetTime, "CloseWorkbook" ' Schedule closing the workbook at the specified time
End Sub
Sub ResetTimer()
On Error Resume Next
Application.OnTime ResetTime, "CloseWorkbook", , False ' Cancel previously scheduled close event
ResetTime = Now + TimeValue("00:00:30") ' Reset timer for another 2 minutes
Application.OnTime ResetTime, "CloseWorkbook" ' Schedule closing the workbook again
End Sub
Sub Workbook_Open()
StartTimer ' Start the timer when the workbook is opened
End Sub
Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
ResetTimer ' Reset the timer when any activity is detected
End Sub
Public Sub StartTimer()
If Not TimerStarted Then
Application.OnTime Now + TimeValue("00:00:30"), "CloseWorkbook"
TimerStarted = True
End If
End Sub
Public Sub CloseWorkbook()
If Now > LastActivity + TimeValue("00:00:30") Then
On Error Resume Next
If Not ThisWorkbook.ReadOnly Then
ThisWorkbook.Save
End If
If Not ThisWorkbook.ReadOnly Then
ThisWorkbook.Close
End If
SaveChanges = False
Else
StartTimer
End If
End Sub
Bookmarks