> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Caching Object Instances in VBA Classes
- URL: https://nolongerset.com/caching-objects-in-vba-classes/
- Published: 2022-02-08T00:58:24.000Z
- Updated: 2026-05-08T12:59:40.000Z
- Description: Simplify the object initialization code in your class modules without sacrificing performance using this easy technique.
- Author: Mike Wolfe
- Tags: Class Modules, Intermediate, #Import 2026-05-20 02:59

When I want to refer to object instances inside of a class module, I tend to use an approach that I call "on-demand initialization":

1. Declare a private object *variable*
2. Declare a public/private object *Property Get*
3. Inside the Get, check to see if the *private variable is nothing*
4. If it is nothing, *instantiate the private variable*
5. *Return the private variable*

Here's what this looks like in practice:

```vba
'--== clsExcel Class Module ==--
Option Compare Database
Option Explicit

Private mExcelApp As Object  'Excel.Application

Public Property Get ExcelApp() As Object
    If mExcelApp Is Nothing Then
        Set mExcelApp = CreateObject("Excel.Application")
    End If
    Set ExcelApp = mExcelApp
End Property

Private Sub Class_Terminate()
    Set mExcelApp = Nothing
End Sub
```

Here's some test code to show how it works:

```vba
Sub TestExcel()
    Dim xl As clsExcel
    
    Set xl = New clsExcel
    Debug.Print "A: "; xl.ExcelApp.Hwnd
    Debug.Print "B: "; xl.ExcelApp.Hwnd
    Set xl = Nothing
    
    Set xl = New clsExcel
    Debug.Print "C: "; xl.ExcelApp.Hwnd
    Set xl = Nothing
End Sub
```

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/02/image-24.png)

The first two calls to ExcelApp.Hwnd return the same window handle because it's the same Excel process. When we create a new instance of clsExcel, we get a new window handle for the second Excel process.

## Why Bother?

I use this approach for two main reasons:

1. It centralizes the object initialization code
2. It improves performance by eliminating unnecessary initialization

There are a few additional benefits to this approach:

- During development, it helps avoid annoying "Object invalid or no longer set" errors when the code gets reset
- It provides an easy way to reset class state (for classes where it makes sense), by having a Reset method that simply sets all the private object variables to Nothing

*Image by [www\_slon\_pics](https://pixabay.com/users/www%5Fslon%5Fpics-5203613/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3948253) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3948253)*