+1  A: 

This looks to be a tough cookie. It's a little hokey, but what you could do is have your Initialize function return a name (string), then add the name parameter to the Get function. Basically manipulating the name string instead of the object directly.

Lance Roberts
yeah, preferably i'd like to avoid this. i have seen a software application do the task i am describing, but 1) i have since forgotten the name and 2) i have no idea what was being done on the back-end that would allow for this behavior. i don't mind learning COM or something equivalent that would enable this extra functionality, but i have no idea where to begin. being able to store user-defined types in a cell really would open up Excel tremendously.
B Rivera
I'm sure with the Variant data type that there would be some way to do this, but I don't have the answer.
Lance Roberts
yeah, i tried everything i could think of. additionally, i can't nest the functions either in an excel cell. (see my commment in the question). i would've thought that was surely possible since the final output of the function is of type Integer. but my guess is that UDFs don't know how to return a type other than the built-in types.
B Rivera
A: 

The nesting won't work because myvar goes out of scope as soon as the UDF is done executing. There actually may be other problems associated with trying to return an object in a worksheet function (most certainly there are), but even if there weren't the scope problem would still kill it.

You could store a pointer to the object in a cell and get the object via that pointer, but again the scope will kill it. To get the object from the pointer, it would have to remain in scope, so why bother storing the pointer.

Obviously your real life situation is more complex than your example. So the answer is no as to storing objects in cells, but if you explain what you're trying to accomplish there may be alternatives.

Dick Kusleika
maybe i don't understand something about UDFs in VBA/Excel, but why would i lose scope? isn't InitializeMyClass returning a MyClass variable? if i had a UDF that returned a double type, then i could certainly nest it with another UDF that accepted a double type as an input variable. i feel that at least nesting of functions should work.by the way, how would i store a pointer to my user-defined-data-type in a cell?
B Rivera
+1  A: 

As the other answers suggest, the literal answer to your question is "no". You can't store anything other than a number, string, boolean, error, etc. in a cell. You can't return anything other than a simple value such as those, or an array, or a range reference, from a UDF.

However, you can do essentially what you want by passing around (and storing in cells) some kind of handle to your objects that is a legal cell value (i.e. "myclass:instance:42"). This is probably what the example you linked to in your edit does. Your code has to be able to interpret the meaning of the handle values and maintain the objects in memory itself, though. This can get tricky if you care about not leaking objects, since there are lots of ways to erase or overwrite handles that you can't detect if you're using VBA to do all this.

I don't have it in front of me right now, but you might want to look at the book Financial Applications using Excel Add-in Development in C/C++ by Steve Dalton:

http://www.amazon.com/Financial-Applications-using-Development-Finance/dp/0470027975/ref=ntt%5Fat%5Fep%5Fdpt%5F1

He discusses ways to work with handles like this more robustly with XLL add-ins.

jtolle
+1 neat, thanks for the link to the book. will definitely look into it.it is too bad that "=GetMyVar(InitializeMyClass(3))" is not possible in an excel cell but GetMyVar(InitializeMyClass(3)) is legal in VBA.
B Rivera
I should also point out a product called Resolver One. It's a Python-based spreadsheet where you *can* store an object in a cell and use it like you want to above. Of course, it's not Excel if that is what you need to use, but you might want to look at it if you're just trying to solve a problem for yourself or if you get to pick the tool your users will be using.
jtolle
holy s***! it's amazing that with all the years of development for excel, it is unable to do what resolver one can do. i might download the free trial for my own use. it may be difficult to convince my client to go with resolver one as he is reasonably entrenched in excel. resolver systems has a pretty good demo for their product.
B Rivera