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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,224,828
Messages
6,181,214
Members
453,024
Latest member
Wingit77

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