PDA

View Full Version : specifying range when returning an array



sandbagger
03-15-2009, 05:44 PM
i have a function that returns a 2-d array of varying size. could be 300x6, 400x4 etc. i simply want to dump that array into the spreadsheet starting at "A1".

for instance,

if my array is a 4x5 array i need:
Range(A1:E4) = someArray

if my array is a 27x10 array i need:
Range(A1:AA10) = someArray

do i have to write some function that takes the dimensions of the array and calculates that the end of the range? like:
27 columns and 10 rows => AA10

if i make the range bigger than the array i get "#N/A" in all the extra cells. there has got to be a better way to do this.

Bob Phillips
03-15-2009, 05:54 PM
Range("A1").Resize(UBound(ary, 2) - LBound(ary, 2) + 1, UBound(ary, 1) - LBound(ary, 1) + 1) = _
Application.Transpose(ary)

sandbagger
03-15-2009, 06:07 PM
i didn't know about "resize". i'm not following the need to transpose but this seems to work:


Range("A1").Resize(UBound(fullArray, 1) - LBound(fullArray, 1) + 1, UBound(fullArray, 2) - LBound(fullArray, 2) + 1) = fullArray


thanks.

Bob Phillips
03-16-2009, 12:58 AM
The reason is so that the rows of the array are put in the rows, and the columns oin the columns, not vice versa.