View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
MrShorty[_9_] MrShorty[_9_] is offline
external usenet poster
 
Posts: 1
Default help again function vectorfunction


It's been a while since I did something very similar to what you are
doing, but I think I can help. I remember fighting VBA for quite
awhile over this very issue. If I remember right, it boils down to the
subtle distinction between "a variant containing an array" and "an array
of variants."

In your code: q and w are typed as "array of variant." There are
several good discussions of the variant data type. Basically it means
that each element of the arrays q and w can contain just about
anything, VBA will figure it out in the context of the variable's use,
and that means that each element can contain an array.

s, and each of the functions third_pow, second_pow, and thirdsecond_pow
are typed as "variants containing arrays." This means that these
"variables" contain a single element which, in these cases, just
happens to be an array.

In the statement "q=second_pow(s)" you are trying to set "an array of
variants" equal to a "variant containing an array." Ultimately, what
you want is for q(n,1)=second_pow(s)(n,1) (which, BTW, if I remember
correctly, is a valid statement, but you end up calling the function a
lot more than necessary). VBA seems to have trouble with this idea. I
think in its mind it is wondering, "which element of the "array of
variants" q do you want me to assign this "variant containing an array"
to?"

Solutions:

1) Algebraically, (a^x)^y=a^(xy), so, if I follow your functions
correctly, you start with s(i) and end up with (s(i)^2)^3. This would
reduce to s(i)^6. For your example, it would be better to eliminate
third_pow and second_pow and simply raise each element of s to the 6th
power. But, this is an example as noted, so this algebraic
simplification may not apply to your real problem.

2) Dimension q and w simply as variants (which can contain arrays),
instead of arrays of variants (Dim q,w as Variant). Then you won't get
the so-called type mismatch in the assignment statements. As I recall,
you'll still access each element of the arrays the same (q(3,1) should
return the third element of the array).

3) Design the inside functions (third_pow, second_pow) so they return a
single element. Example:

calling statement:
q(n,1)=second_pow(s(n,1))

Function second_pow(arg as double) as double
second_pow=arg^2
end function

I'm sure with more details about your real application, someone here
could give you better solutions. Hopefully, by looking at your code
and what you want it to accomplish, one of these solutions will help.


--
MrShorty
------------------------------------------------------------------------
MrShorty's Profile: http://www.excelforum.com/member.php...o&userid=22181
View this thread: http://www.excelforum.com/showthread...hreadid=382979