Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Read a range to an array

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.


  #2   Report Post  
Posted to microsoft.public.excel.programming
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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Read a range to an array

I can't say why, but I always cringe when I see a function name used as a
variable - nonetheless, it works in many cases. Apparently this isn't one
of those cased. Yes, in general I would say only a variant can be used to
pass an array (or a variant array in Excel 2000 or later).

This works:

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

--
Regards,
Tom Ogilvy


"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.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Read a range to an array

Hi Tom,

So do you mean that I must declare a variant variable within a function, and
finally assign that (variant) array back to the function? Seems redundant to
me.

Frederick Chow

"Tom Ogilvy" wrote in message
...
I can't say why, but I always cringe when I see a function name used as a
variable - nonetheless, it works in many cases. Apparently this isn't one
of those cased. Yes, in general I would say only a variant can be used
to
pass an array (or a variant array in Excel 2000 or later).

This works:

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

--
Regards,
Tom Ogilvy


"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.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Read a range to an array

Don't do it then.

--
Regards,
Tom Ogilvy

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

So do you mean that I must declare a variant variable within a function,

and
finally assign that (variant) array back to the function? Seems redundant

to
me.

Frederick Chow

"Tom Ogilvy" wrote in message
...
I can't say why, but I always cringe when I see a function name used as a
variable - nonetheless, it works in many cases. Apparently this isn't

one
of those cased. Yes, in general I would say only a variant can be used
to
pass an array (or a variant array in Excel 2000 or later).

This works:

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

--
Regards,
Tom Ogilvy


"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.








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
How to Read Worksheet Data into VBA Array? Peter Bernadyne Excel Discussion (Misc queries) 1 July 12th 06 05:19 PM
Excel, read in an array AustinJames Setting up and Configuration of Excel 4 September 20th 05 03:18 PM
Read comboboxes into array Steph[_3_] Excel Programming 3 August 11th 04 04:40 PM
Read in Array Leigh Excel Programming 1 May 5th 04 09:18 AM
Read Range Data into Array Stratuser Excel Programming 1 April 26th 04 06:46 PM


All times are GMT +1. The time now is 02:47 PM.

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

About Us

"It's about Microsoft Excel"