Workbook with Timer Macro Saving After Closing

vettaforza

New Member
Joined
Dec 6, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that has a timer macro in it that will save and close the book after 2 minutes of inactivity. That works fine. When you have another instance of excel open, and you close the workbook that has the timer macro in it, the workbook with the timer macro will still save, you can see it pop up briefly on the other excel sheet you have open. I'm not sure what to do.

VBA Code:
Option Explicit
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


VBA Code:
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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
When you have another instance of excel open, and you close the workbook that has the timer macro in it, the workbook with the timer macro will still save, you can see it pop up briefly on the other excel sheet you have open.
You need to cancel the timer when the workbook is closed.

In the ThisWorkbook module:

VBA Code:
Sub Workbook_Close()
    On Error Resume Next
    Application.OnTime ResetTime, "CloseWorkbook", , False ' Cancel previously scheduled close event
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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