Excel VBA Help: Passing Objects as Class Subroutine Arguments

Farmer

Diamond Member
Dec 23, 2003
3,334
2
81
I have two classes, A and B, each with a few properties and get or get/let methods for those properties. Suppose that class A has Height, Length and Width properties/class variables.

Class A also has a method aMethod which takes in a class B object as an input parameter and does not return anything. It doesn't matter the details of the method, as the problem occurs even if I have a blank method.

Because there are no constructors per se in VBA, when I create an object I establish values for its property via something like this:

Code:
Dim Alpha As A
Set Alpha = New A
With Alpha
        .Height = 1
        .Length = 1
        .Width = 1
End With

Now, suppose Beta is an instance of class B. If I call

Code:
 Alpha.aMethod Beta

I get a "Runtime Error 438 : Object doesn't support this property or method."

If I don't set the properties of Alpha. I.e., if I just say "Set Alpha = New A," there is no error. The class method is nothing more than blank code.

Furthermore, if the method takes a non-object input, say, an Integer, it works fine regardless of whether or not I set the properties.

Code:
 Public Sub aMethod ( Object As B )

End Sub

In summary, the below returns "Runtime Error 438"

Code:
Dim Alpha As A
Dim Beta As B

Set Beta = New B
Set Alpha = New A
With Alpha
        .Height = 1
        .Length = 1
        .Width = 1
End With

Alpha.aMethod Beta

While this runs without error:

Code:
Dim Alpha As A
Dim Beta As B

Set Beta = New B
Set Alpha = New A

Alpha.aMethod Beta

Why does setting the properties of an object instance break this code? What is the proper way of creating object instances in VBA? How do I pass object as arguments to class methods?

Any help would be appreciated. I am writing a simple macro and I'm just getting hung up on this language.
 
Last edited:

Farmer

Diamond Member
Dec 23, 2003
3,334
2
81
Ok, so if I create a "constructor" method that initializes all the parameters, i.e.,

Code:
Public Sub init(Length As Double, Width As Double, Height As Double)

pLength = Length
pWidth = Width
pHeight = Height

End Sub

And remember to run it everytime I instantiate a class, it works fine.

There is still a minor error where strings can't get assigned to properties (WTF).

WTH is wrong with the let method? Why does it break this?
 
Last edited: