title: Macro in Excel to save every 30 minutes and close after 35 minutes of no use. I need to unload ThisWorkbook, but can't figure out how
body: <p>VBA code for autosaving and for closing the workbook if idle is working. The problem is, Excel continues to run the code if another instance of the program was open when the code closed the workbook. I think what I need to do is to unload the workbook, but I can't figure out how. I've tried &quot;Unload Workbook,&quot; &quot;Unload ThisWorkbook,&quot; and &quot;Unload ResetTimer&quot; [the module which detects activity and starts the 35 minute timer over]. I'm getting an error that Workbook/ThisWorkbook/ResetTimer are not object that can be unloaded. I can't find a list of what objects <em>can</em> be unloaded.</p>
  <p>Here is the code under ThisWorkbook</p>
  <pre><code>Option Explicit
  
  Private Sub ThisWorkbook_Open()
      If ThisWorkbook.ReadOnly = False Then
      Application.OnTime Now + TimeValue(&quot;00:30:00&quot;), &quot;SaveThis&quot;
      End If
  
      If ThisWorkbook.ReadOnly = False Then
      Application.OnTime Now + TimeValue(&quot;00:35:00&quot;), &quot;CloseDownFile&quot;
      End If
  End Sub
  
  Private Sub ThisWorkbook_Close()  
      Unload ThisWorkbook
  '    Unload ResetTimer
  End Sub
  
  Private Sub ThisWorkbook_SheetCalculate(ByVal Sh As Object)
  ResetTimer
  End Sub
  
  Private Sub ThisWorkbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  ResetTimer
  End Sub
  
  Private Sub ThisWorkbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  ResetTimer
  End Sub
  </code></pre>
  <p>Here is the Module:</p>
  <pre><code>Public CloseDownTime As Variant
  
  Public Sub ResetTimer()
  On Error Resume Next
  If Not IsEmpty(CloseDownTime) Then Application.OnTime EarliestTime:=CloseDownTime, Procedure:=&quot;CloseDownFile&quot;, Schedule:=False
  CloseDownTime = Now + TimeValue(&quot;00:35:00&quot;) ' change as needed
  Application.OnTime CloseDownTime, &quot;CloseDownFile&quot;
  End Sub
  
  Public Sub CloseDownFile()
  On Error Resume Next
  ThisWorkbook.Close SaveChanges:=True
  Unload ThisWorkbook
  End Sub
  
  Sub SaveThis()
      Application.DisplayAlerts = False
      ThisWorkbook.Save
      Application.DisplayAlerts = True
  
  Application.OnTime Now + TimeValue(&quot;00:30:00&quot;), &quot;SaveThis&quot;
  End Sub
  </code></pre>