Question

This is for Excel and VBA. Assume that BondClass has been properly defined in a Class Module. I get a #VALUE! when I type "=GetBondPrincipal()" into an Excel cell. Have I done something syntactically wrong or is this just not possible in Excel/VBA? I ask because what I really want to do is this: Return a User Defined Data Type in an Excel Cell

but am unable to find a solution. So at the very least, I want to know if what I want to do below is possible.


Option Explicit

Function InitializeBond(ir As Double, p As Double) As BondClass Dim mybond As BondClass Set mybond = New BondClass Call mybond.Initialize(ir, p) InitializeBond = mybond End Function

Function GetBondPrincipal() Dim b As BondClass Set b = New BondClass b = InitializeBond(0.03, 100) //the code quits here, //it doesn't like the BondClass return type? GetBondPrincipal = b.GetPrincipal() End Function

I know that in the example I have provided, I don't have to call InitializeBond and can simply type "Call b.Initialize(.03,100)". The code will work fine if I do this. But I can't seem to be able to get a UDF to return a type other than the built-in types. Any way to do any of this? Do I have to define assignment for non-built-in types?

Was it helpful?

Solution

Hint: use "SET".

(most common error made by VB programmers in VB6 and VBA).

OTHER TIPS

Anyway, I don't find that very elegant.
I would rather write:

Function GetBondPrincipal()
   Dim b As BondClass
   Set b = New BondClass
   with b
     .param1 = 0.03
     .param2 = 100
     .InitializeBond
     GetBondPrincipal = .GetPrincipal()
   end with
End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top