title: VBA memory issue while assigning Range with array
body: <p>My purpose is to assign an Excel range of cells with the values of a two dimensional array of variants.</p>
  
  <p>The assignement works fine, but I face a memory problem (leak) : when I launch Excel, the process takes more or less 22 Mo of Ram. After running the following code (creating a new workbook, assigning the range, closing the workbook without saving), the process takes 33 Mo of RAM.</p>
  
  <p>Does anyone have an idea what I am doing wrong ?</p>
  
  <p>Regards,
  Alexander</p>
  
  <p>Here the code</p>
  
  <pre><code>Option Explicit
  
  Sub test()
     Dim nLines As Long
     Dim xSize As Long
     Dim j As Long
     Dim i As Long
  
     'Creating New Empty Workbook
     Application.Workbooks.Add
     Range("A1").Select
  
     Application.ScreenUpdating = False
  
     nLines = 10000
     xSize = 200
  
     Dim myRange As Range
     Dim myArray() As Variant
  
     ReDim myArray(1 To nLines, 1 To xSize)
  
     'Assigning some values
     For j = 1 To nLines:
         For i = 1 To xSize:
          myArray(j, i) = i * 4 / 3#
         Next i
     Next j
  
     Set myRange = Range(ActiveCell, ActiveCell.Offset(nLines - 1, xSize - 1))
     myRange .Value = myArray
  
     'Cleaning up
      Erase myArray
      Set myRange = Nothing
  
      'Closing workbook without saving
      Application.ActiveWorkbook.Close (False)
  
      Application.ScreenUpdating = True
  
  End Sub
  </code></pre>