Dim clsOne As Class1
Dim clsTwo As New Class1
Those are two ways to declare a variable as a class. The first way simply data types the variable; always a good coding practice. The second way includes the New keyword which data types the variable and directs the compiler to instantiate the class whenever it’s used.
Without New you would, later in your code, use a statement like
Set clsOne = New Class1
to instantiate the class manually. With New, VBA instantiates the class whenever you need it. That may be convenient, but it adds unnecessary code. As Chip Pearson put it
So when you have code like
Dim X As New Class1
X.Property = 123
the compiler actually creates the code
Dim X As Class1
If X Is Nothing Then
Set X = New Class1
X.Property = 123
If you’re only referencing X’s property once, then you’ve lost nothing by using New in the Dim statement. However, that extra bit of code the compiler adds is added to every line that references that class. The compiler has to check every time. It’s far more efficient for you to check when appropriate.
Based on Chip’s explanation, I can’ t think of any reason to ever use the New keyword in a Dim statement. (To be sure, I’ve been guilty of it, but I always try to control when the class is created rather than let VBA do it. ) If you put that If block before every class reference but one, you’d be better off than using New, if only a microsecond better.
The rule of the day is: Never use New in a Dim statement. If your code has multiple places where a class can be created, include the test before the first use in each of those places. Once you’ve referenced the class, you shouldn’t need to test it anymore.