When you create an Excel automation object using the
CreateObject function, the automation object remains in memory until you set it equal to the keyword
Nothing. However, when you reference the object using a
With statement, the object remains in memory until the subroutine ends. In order to control the release of the Excel automation object from memory, do not use the
With statement when referring to the Excel object. Instead, explicitly refer to the object by its variable name. For example, with an Excel automation object named myExcel, the following statements illustrate the preferred way to refer to the automation object:
Use this:
myExcel.Workbooks(1).Worksheets(1).Cells(1,1).Value = "Hello"
myExcel.Workbooks(1).Worksheets(1).Cells(2,1).Value = "World"
Instead of:
With myExcel.Workbooks(1).Worksheets(1)
.Cells(1,1).Value = "Hello"
.Cells(2,1).Value = "World"
End With