title: VBA preserve instances after macro completes (Excel)
body: <p>I have some objects of my own that I instantiate inside <code>ThisWorkbook</code> in Excel. I'm having <a href="https://stackoverflow.com/questions/13365758/vba-change-instance-variable-from-module-excel">some trouble</a> and I think the issue is that VBA is killing my instances as soon as the macro ends.</p>
  
  <p>We're using <code>ThisWorkbook</code>:</p>
  
  <ul>
  <li><p>If I define my own variable for <code>ThisWorkbook</code> in a macro, that variable's value is back to the initialization value after the macro completes, even if the variable is Dim'd in the class of <code>ThisWorkbook</code> and not in the module. (i.e. 0, Nothing, etc.)</p></li>
  <li><p>If I instantiate an object for <code>ThisWorkbook</code> in the <code>Workbook_Open()</code> event, the object is destroyed when the event is done firing. Again, even if the variable is Dim'd in the class of <code>ThisWorkbook</code> and not in the sub for <code>Workbook_Open()</code></p></li>
  </ul>
  
  <p>Is there something I can do to make sure these objects survive and these variables retain their values?</p>
  
  <p>In <code>ThisWorkbook</code>:</p>
  
  <pre><code>'Object we want to survive
  Dim myInstance As MyObject
  
  Private Sub Open_Workbook()
      Set myInstance = new MyObject ' Object is instantiated
  End Sub
  
  ' Macro 1
  Public Sub MyMacro()
      Set myInstance = new MyObject ' Object is instantiated
  End Sub
  
  ' Macro 2
  Public Sub CheckInstance()
      If Not myInstance is Nothing Then
          MsgBox "I found an instance!"
      End If
  End Sub
  </code></pre>
  
  <p>Neither opening the workbook nor running Macro 1 will cause Macro 2 to find myInstance. In both cases, Macro2 will think that myInstance is Nothing. Macro 2 will ONLY find myInstance if it's called inside Macro 1 or Open_Workbook (before their End Sub). I need a fix for this. (I'm running these macros from separate form buttons, NOT inside some other macro.)</p>