ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   help again function vectorfunction (https://www.excelbanter.com/excel-programming/333096-help-again-function-vector-function.html)

pino

help again function vectorfunction
 
I dont understand the help ...............ParamArray intScores() As
Variant)...
How do you modify this example that...doesnt work :-(

Here, the function thirdsecond_pow does not work
Can you help me more?

thanks a lot!

Option Base 1
Function thirdsecond_pow(s As Variant)


Dim w(), q()
n = s.Rows.Count


ReDim w(n, 1), q(n, 1)


q = second_pow(s)
w = third_pow(q)


thirdsecond_pow = w


End Function


Function second_pow(s As Variant)


Dim w()
n = s.Rows.Count
ReDim w(n, 1)


For i = 1 To n
w(i, 1) = s(i) ^ 2
Next i


second_pow = w


End Function


Function third_pow(s As Variant)


Dim w()
n = s.Rows.Count


ReDim w(n, 1)
For i = 1 To n
w(i, 1) = s(i) ^ 3
Next i


third_pow = w


End Function


thank you



Bob Phillips[_6_]

help again function vectorfunction
 
You don't say what you are trying to do, or in what way it doesn't work. I
think that would be helpful.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"pino" wrote in message
...
I dont understand the help ...............ParamArray intScores() As
Variant)...
How do you modify this example that...doesnt work :-(

Here, the function thirdsecond_pow does not work
Can you help me more?

thanks a lot!

Option Base 1
Function thirdsecond_pow(s As Variant)


Dim w(), q()
n = s.Rows.Count


ReDim w(n, 1), q(n, 1)


q = second_pow(s)
w = third_pow(q)


thirdsecond_pow = w


End Function


Function second_pow(s As Variant)


Dim w()
n = s.Rows.Count
ReDim w(n, 1)


For i = 1 To n
w(i, 1) = s(i) ^ 2
Next i


second_pow = w


End Function


Function third_pow(s As Variant)


Dim w()
n = s.Rows.Count


ReDim w(n, 1)
For i = 1 To n
w(i, 1) = s(i) ^ 3
Next i


third_pow = w


End Function


thank you





Pino

help again function vectorfunction
 
Well,
the function thirdsecond_pow(s) (s is a vector, a coloumn) give me an
erron (#VALUE) it should give me
the power 3 of the vector q that is the vector output of the function
second__pow

The problem is:
1) a call the function second_pow(s) it gives me the vector q.
q=second_pow(s)..OK
2) when I try to apply the function third_pow() to q (w= third_pow(q))
...it does not work!
try to cut and paste the code on on your vba editor.

This is an example...the code I am developing in not about powers :-)!





Bob Phillips ha scritto:
You don't say what you are trying to do, or in what way it doesn't work. I
think that would be helpful.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"pino" wrote in message
...
I dont understand the help ...............ParamArray intScores() As
Variant)...
How do you modify this example that...doesnt work :-(

Here, the function thirdsecond_pow does not work
Can you help me more?

thanks a lot!

Option Base 1
Function thirdsecond_pow(s As Variant)


Dim w(), q()
n = s.Rows.Count


ReDim w(n, 1), q(n, 1)


q = second_pow(s)
w = third_pow(q)


thirdsecond_pow = w


End Function


Function second_pow(s As Variant)


Dim w()
n = s.Rows.Count
ReDim w(n, 1)


For i = 1 To n
w(i, 1) = s(i) ^ 2
Next i


second_pow = w


End Function


Function third_pow(s As Variant)


Dim w()
n = s.Rows.Count


ReDim w(n, 1)
For i = 1 To n
w(i, 1) = s(i) ^ 3
Next i


third_pow = w


End Function


thank you




MrShorty[_9_]

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


Pino

help again function vectorfunction
 
Thank you very much!
I find a simple solution! I was calling thirdsecond_pow(s() As Variant)
selecting a range on the sheet1.
So there was a type mismatch!
Now set thirdsecond_pow(s as range) and after i declare a variant A
and use the command A=s.
Now s can be applied to all the function.
Of course there was the error of the Ubound ...

Regards



All times are GMT +1. The time now is 12:03 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com