View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Leo Heuser[_3_] Leo Heuser[_3_] is offline
external usenet poster
 
Posts: 109
Default passing named range to a UDF user defined function

Hello Brian

If I have understood you correctly, here's one way to do it:

Function myFunc(od As Range) As Double
Dim Cell As Range
For Each Cell In od.Cells
myFunc = myFunc + 3.14 / 4 * Cell.Value ^ 2
Next Cell
End Function


--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"Brian Murphy" skrev i en meddelelse
...
Hello Group,

I'm stuck on something I thought was going to be easy.

I have a named range called OD that refers to a column of values.

I have a user defined function called myFunc that wants to take a single
value as an argument, and return a single value.

Function myFunc(od) As Double
myFunc = 3.14 / 4 * od ^ 2
End Function

I can call myFunc if the argument is a value or a reference to a single
cell, but not with the named range OD as the argument. I get #VALUE!

Is it possible to call my function with OD as an argument? I can pass OD as
an argument to excel's built in functions without any trouble.

If necessary, I think it might be okay if my function were changed to return
an entire column of values. I tried the following, but it didn't work
either.

Function myFunc(od) As Variant
Dim i%
ReDim arr(1 To od.count)
For i = 1 To od.count
arr(i) = 3.14 / 4 * od(i) ^ 2
Next
myFunc = arr
End Function

Can someone please set me straight.

Thanks,

Brian Murphy
Austin, Texas