How do I work with user-defined types?

1.1k views Asked by At

Every time I try to pass a UDT around to other functions I get this compile error: "Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions."

The only internal function that I've tested that works with UDTs as arguments is VarPtr. I can't even use TypeName or VarType.

Is there a way to pass UDTs as arguments that are not explicitly expecting the specific type? Kind of like Variant but for UDTs.

I would also like to store UDTs in arrays/dictionaries/collections, but that also seems to be problematic. UDTs sound perfect for my needs (not having to have a separate class module), but it seems like they are very strictly-typed (if that's the correct term for it).

EDIT: It looks like VarPtr expects the argument as the "Any" type, so maybe that's the type that accepts Variants and UDTs. Problem is, the VBA IDE built into Excel doesn't allow me to use that type.

1

There are 1 answers

0
TheSilkCode On

So you can only use UDT within the module they are declared in- so most likely you declared your UDT in one module and the error is being generated when a function in a different module tries to use your UDT...

I think based on your question you are looking for custom classes which are similar to UDTs but they can be used throughout your project and even passed as parameters to and from functions like you are looking for.

Hope this helps- TheSilkCode