Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array UDF

2 views
Skip to first unread message

Dileepan

unread,
Oct 3, 2003, 10:57:43 PM10/3/03
to
Hi,

I have an array UDF that displays value from the first
cell into all the cells in the array range. With MsgBox
I know the values are getting computed correctly. Please
let me know what I am doing wrong.

The relevant portion of the UDF is given below.

======================================
Function rkArray(dataArray As Range, K As Integer) As
Variant

.... statements .....

ReDim rk(K)

.... statements computing rk array

rkArray = rk
End Function
======================================

-- Dileepan

John Green

unread,
Oct 4, 2003, 3:56:10 AM10/4/03
to
Your code is only going to work for an array function entered into a single row of cells. Also, if you are using the default zero
based arrays, rk will contain K+1 elements.

A more general treatment of rows and columns is shown below where Application.Caller is used to discover the range into which the
array function is entered and the array is filled with numbers. It works with single row, single column arrays as well as arrays
with multiple rows and columns.

Function rkArray() As Variant

Dim rk()
Dim rArray As Range
Dim NumRows As Long
Dim NumColumns As Long
Dim i As Long
Dim j As Long

Set rArray = Application.Caller
NumRows = rArray.Rows.Count
NumColumns = rArray.Columns.Count

ReDim rk(1 To NumRows, 1 To NumColumns)

For i = 1 To NumRows
For j = 1 To NumColumns
rk(i, j) = i + j
Next j
Next i

rkArray = rk
End Function


--

John Green - Excel MVP
Sydney
Australia


"Dileepan" <dile...@utc.edu> wrote in message news:1fab501c38a23$482db7b0$a601...@phx.gbl...

J.E. McGimpsey

unread,
Oct 4, 2003, 4:04:10 AM10/4/03
to
If you're creating a one dimensional array, you need to transpose it
if you're returning the values to a column. Here's one way:

Function rkArray(dataArray As Range, K As Integer) As Variant

Dim rk As Variant
Dim i As Long
ReDim rk(1 To K)
For i = 1 To UBound(rk)
rk(i) = dataArray.Cells(i).Value * i
Next i
With Application.Caller
If .Rows.Count = 1 Then
rkArray = rk
ElseIf .Columns.Count = 1 Then
rkArray = Application.Transpose(rk)
Else
rkArray = CVErr(xlErrNA)
End If
End With
End Function

In article <1fab501c38a23$482db7b0$a601...@phx.gbl>,

0 new messages