Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
snv snv is offline
external usenet poster
 
Posts: 3
Default Getting #VALUE! while entering UDF as an array function

I am trying to enter my own function in an array formula context,
similar to any other pre defined funtion.
For example, let my array be
{1;4;9;16;25;36}
If I select a 6x1 range of cells and enter the formula
=SQRT({1;4;9;16;25;36})
with a ctrl+shift+enter
I get
1
2
3
4
5
6

Now I want to write a UDF that does the same thing

Function myUDF(Input As Variant) As Variant
myUDF = Input ^ 0.5
End Function

when I enter
=myUDF({1;4;9;16;25;36})
again, with ctrl+shift+enter

I get
#VALUE!
#VALUE!
#VALUE!
#VALUE!
#VALUE!
#VALUE!

Interestingly enough when I enter
=myUDF(81)
I get
9

Had myUDF been

Function myUDF(Input As Variant) As Variant
myUDF = Input
End Function

IE, no modification, just return what I get
=myUDF({1;4;9;16;25;36})
returns
1
4
9
16
25
36

as expected.

The problem seems to be when I try to use myUDF in an array context
and I try to modify the input in any way. How can I get it to act
just like the
=SQRT({1;4;9;16;25;36})

And before anyone asks why I trying to emulate an already existing
function, this is just an example to illustrate the problem.

Please help and thanks in advance

Sean
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Getting #VALUE! while entering UDF as an array function

On Thu, 7 Feb 2008 10:28:52 -0800 (PST), snv wrote:

I am trying to enter my own function in an array formula context,
similar to any other pre defined funtion.


Take a look here for some information:

http://www.cpearson.com/excel/return...ysfromvba.aspx

What you need to do is test your argument(s) to see if they are arrays or not;
and if they are, you need to set up an array within your code; and then equate
it to the function name.
--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
snv snv is offline
external usenet poster
 
Posts: 3
Default Getting #VALUE! while entering UDF as an array function

On Feb 7, 10:45 am, Ron Rosenfeld wrote:
On Thu, 7 Feb 2008 10:28:52 -0800 (PST), snv wrote:
I am trying to enter my own function in an array formula context,
similar to any other pre defined funtion.


Take a look here for some information:

http://www.cpearson.com/excel/return...ysfromvba.aspx

What you need to do is test your argument(s) to see if they are arrays or not;
and if they are, you need to set up an array within your code; and then equate
it to the function name.
--ron


Thanks for the response Ron. A coworker of mine and I just came up
with that same answer.

When I enter the UDF as an array function the entire array is passed
as a single parameter.
When I enter the pre defined excel function as an array function it
behaves as a loop where in invokes the function once for each element
of the array. So the suggested solution works, but can a UDF be
flagged to behave the same as a pre defined function?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Getting #VALUE! while entering UDF as an array function

On Thu, 7 Feb 2008 11:08:53 -0800 (PST), snv wrote:

On Feb 7, 10:45 am, Ron Rosenfeld wrote:
On Thu, 7 Feb 2008 10:28:52 -0800 (PST), snv wrote:
I am trying to enter my own function in an array formula context,
similar to any other pre defined funtion.


Take a look here for some information:

http://www.cpearson.com/excel/return...ysfromvba.aspx

What you need to do is test your argument(s) to see if they are arrays or not;
and if they are, you need to set up an array within your code; and then equate
it to the function name.
--ron


Thanks for the response Ron. A coworker of mine and I just came up
with that same answer.

When I enter the UDF as an array function the entire array is passed
as a single parameter.
When I enter the pre defined excel function as an array function it
behaves as a loop where in invokes the function once for each element
of the array. So the suggested solution works, but can a UDF be
flagged to behave the same as a pre defined function?


One way that I know of is to check in the UDF to see what type of variable has
been passed. You can use the VarType function for this. Then you process it
accordingly.
--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Getting #VALUE! while entering UDF as an array function

On Thu, 7 Feb 2008 11:08:53 -0800 (PST), snv wrote:

On Feb 7, 10:45 am, Ron Rosenfeld wrote:
On Thu, 7 Feb 2008 10:28:52 -0800 (PST), snv wrote:
I am trying to enter my own function in an array formula context,
similar to any other pre defined funtion.


Take a look here for some information:

http://www.cpearson.com/excel/return...ysfromvba.aspx

What you need to do is test your argument(s) to see if they are arrays or not;
and if they are, you need to set up an array within your code; and then equate
it to the function name.
--ron


Thanks for the response Ron. A coworker of mine and I just came up
with that same answer.

When I enter the UDF as an array function the entire array is passed
as a single parameter.
When I enter the pre defined excel function as an array function it
behaves as a loop where in invokes the function once for each element
of the array. So the suggested solution works, but can a UDF be
flagged to behave the same as a pre defined function?


Also, in addition to the above reference from Chip Pearson, you should also
familiarize yourself with:

http://www.cpearson.com/Excel/VBAArrays.htm
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Entering an array formula wthawaii Excel Worksheet Functions 2 August 28th 07 01:54 PM
entering as an array Alfonso Excel Discussion (Misc queries) 0 October 5th 06 04:48 PM
Entering Array Functions Jaytee Excel Discussion (Misc queries) 1 August 28th 05 04:11 PM
Entering Array Formula No Name Excel Programming 1 September 1st 04 02:04 PM
Programatically Entering an Array Formula John C.[_4_] Excel Programming 1 July 28th 04 01:53 AM


All times are GMT +1. The time now is 03:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"