I'm having a bit of a weird issue with a particular spreadsheet. It's used by a lot of people so I've tried to get it to close down after 10 minutes of inactivity, as well as do a couple of other little things (mainly remove any filters that have been applied) before it closes.
The trouble I'm having is that the timed inactivity loop seems to remain active even after the spreadsheet has closed. When the loop runs out the macro manages to reload the spreadsheet but then produces an error because the variable is blank (presumably since the spreadsheet has been closed).
I can't work out what's going on. Here's the code in ThisWorkbook:
Private Sub Workbook_Open()
Call SetTimer
' Set variable title equal to exact application window title
title1 = Application.Caption
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets("INVOICE LOG").FilterMode = True Then
Sheets("INVOICE LOG").ShowAllData: End If
ActiveWindow.Zoom = 100
Call StopTimer
End
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Call StopTimer
Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Call StopTimer
Call SetTimer
End Sub
and here's what's in a module:
Option Explicit
Dim DownTime As Date
Public title1 As String
Sub SetTimer()
DownTime = Now + TimeValue("00:10:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=False
End Sub
Sub ShutDown()
'Application.DisplayAlerts = False
' Activate Microsoft Excel again
AppActivate title1
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
'Set the message box to close after about 10 seconds or so
AckTime = 10
If InfoBox.Popup("Export Log will close WITHOUT SAVING in 10 seconds unless you click OK!", _
AckTime, "Warning!", 0) = vbOK Then
DownTime = Now + TimeValue("00:10:00")
Application.OnTime EarliestTime:=DownTime, _
Procedure:="ShutDown", Schedule:=True
Exit Sub
Else: End If
With ThisWorkbook
.Saved = True
.Close
End With
End Sub
Any thoughts would be appreciated. Thanks!
Bookmarks