title: Module variables don't survive CodeModule.InsertLines call
body: <p>I am trying to add a button to my worksheet during run-time. This button should just display different worksheet that is also created on  during run-time. I added button like this:</p>
  
  <pre><code>Dim btnShowTable
  Set btnShowTable = ActiveSheet.Buttons.Add(rowRange.Left + 10, rowRange.Top + 10, rowRange.Width - 20, rowRange.Height - 20)
  btnShowTable.Caption = "Show table data"
  btnShowTable.OnAction = AddClickHandler_ShowSheet("ClickModule", "TableView", tableSheet)
  
  
  Function AddClickHandler_ShowSheet(ByVal moduleName As String, ByVal btnName As String, ws As Worksheet)
    Dim methodName As String
    methodName = btnName &amp; "_" &amp; AddClickHandler_GetId() &amp; "_Click"
    Dim LineNum As Long
    Dim VBCodeMod As CodeModule
    Set VBCodeMod = ThisWorkbook.VBProject.VBComponents(moduleName).CodeModule
    With VBCodeMod
      LineNum = .CountOfLines + 1
        .InsertLines LineNum, _
          "Sub " &amp; methodName &amp; "()" &amp; Chr(13) &amp; _
          "    " &amp; ws.CodeName &amp; ".Select" &amp; Chr(13) &amp; _
          "End Sub"
    End With
    AddClickHandler_ShowSheet = moduleName &amp; "." &amp; methodName
  End Function
  </code></pre>
  
  <p>Function that creates the button is in one module while AddClickHandler_ShowSheet is in another.</p>
  
  <p>The idea is that I would have separate module that would contain all these click handlers so that I could easily delete all of them.
  This works ok. The handlers are created and buttons work as expected. The issue that I have is that, when this InsertLines method is called, all of my module variables in a module that contains the function for button creation are lost.
  I have 4 module variables</p>
  
  <pre><code>Dim xmldoc As New MSXML2.DOMDocument
  Dim xmlDataMap() As DataNode
  Dim xmlDataMapLast As Integer
  Dim xmlTables As Collection
  </code></pre>
  
  <p>after a call to InsertLines all of them became empty except for xmlDataMapLast  which contains correct value of 14.</p>
  
  <p>If I try to debug this method when I step over InserLines call I get an error "Can't enter break mode at this time." and I can't debug anything until my function ends. If I comment out the call to AddClickHandler_ShowSheet my variables remain intact, so it must be something related to that call.</p>
  
  <p>Am I trying to achieve the impossible or am I just doing it the wrong way?</p>