View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Otto Moehrbach[_6_] Otto Moehrbach[_6_] is offline
external usenet poster
 
Posts: 201
Default Read a range to an array

You say:
ReDim ArrayFromRange(Rng.Rows.count, Rng.Columns.count)
The lower bound of the array, as written, is Rng.Rows.Count.
The upper bound is Rng.Columns.Count.
The number of items in any array is (Upper Bound)-(Lower Bound).
IOW, the number of items in your array IAW the Redim statement you have is
equal to the number of columns less the number of rows. I don't think this
is what you want. HTH Otto

"Microsoft Forum" wrote in message
...
Hi all,

I know the following code will work fine for me:

Function ArrayFromRange(Rng As Range) As Variant
ArrayFromRange = Rng
End Function

But I just want to know why the following alternative doesn't work,
especially why the REDIM statement is invalid he

Function ArrayFromRange(Rng As Range) As Variant
Dim i As Long, j As Long
ReDim ArrayFromRange(Rng.Rows.count, Rng.Columns.count)
For i = 1 To Rng.Rows.count
For j = 1 To Rng.Columns.count
ArrayFromRange(i, j) = Rng(i, j)
Next
Next
End Function

By the way, if I wish a function to return an array, is declaring the
function as type "variant' the only way?

Thanks for your advice.

Frederick Chow
Hong Kong.