Using arrays with ranges

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Using arrays with ranges

Post by agibsonsw »

Hello.

I know that I can use a Variant to grab an Excel range of data, and eventually feed it back into a range of the same size:

Dim arr As Variant
arr = Range("A1:H12").Value
'navigate the array using arr(x,y)
Range("A14:H25").Value = arr

But is it possible to grab a "row" or "column" of data from the array and put it into a range, without looping?
Is it possible to grab the Excel range of data and chuck it into a 3D array? I believe it would always coerce the
Variant into a 2D array, but perhaps there is a way to then copy this array into another (3D) array, without looping?

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78508
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using arrays with ranges

Post by HansV »

You can assign a two-dimensional array to a "smaller" range, e.g. a single-row column or a single-column row. The range will be filled with the "top left corner" of the array:

Dim arr As Variant
arr = Range("A1:H12").Value
' Fill a single row with the values of A1:H1
Range("A14:H14").Value = arr
' Fill a single column with the values of A1:A12
Range("K5:K16").Value = arr

But I don't see an easy way to refer to an arbitrary sub-array without looping, nor to copy the array into a three-dimensional array without looping.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Using arrays with ranges

Post by agibsonsw »

Thanks @Hans.

Didn't think so :cheers:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

Jim Cone
StarLounger
Posts: 78
Joined: 18 Feb 2010, 01:44

Re: Using arrays with ranges

Post by Jim Cone »

It is possible to extract any row or column from an array using the Index worksheet function...

Range("A14:H14").Value = Application.WorksheetFunction.Index(arr, 3, 0)
(extracts the the third row)
Range("K5:K16").Value = Application.WorksheetFunction.Index(arr, 0, 3)
(extracts the third columh)

The trick is to specify 0 for row to return the entire column and vice versa.
'---
Jim Cone
Portland, Oregon USA
https://goo.gl/IUQUN2" onclick="window.open(this.href);return false; (Dropbox)
Last edited by Jim Cone on 14 Oct 2016, 13:34, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78508
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using arrays with ranges

Post by HansV »

Thanks, Jim - I always forget about using 0 with the Index function.
Best wishes,
Hans

Jim Cone
StarLounger
Posts: 78
Joined: 18 Feb 2010, 01:44

Re: Using arrays with ranges

Post by Jim Cone »

Hans,
I come here to remember the things I forgot.
You are ahead of me about 20 to 1.

I have been working on a vba Date Picker that requires extraction of columns from an array.
It's easy to remember something when you have just done it. <grin>
Jim Cone
'---
Edit:
I looked at that second line again and it may not convey what I intended which is...
I learn about 20 things here for every item I might contribute.
JBC
Last edited by Jim Cone on 19 Oct 2011, 02:48, edited 1 time in total.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Using arrays with ranges

Post by agibsonsw »

Jim Cone wrote:It is possible to extract any row or column from an array using the Index worksheet function...

Range("A14:H14").Value = Application.WorksheetFunction.Index(arr, 3, 0)
(extracts the the third row)
Range("K5:K16").Value = Application.WorksheetFunction.Index(arr, 0, 3)
(extracts the third columh)

The trick is to specify 0 for row to return the entire column and vice versa.
Jim Cone
All I can say is Wow! and thank you :clapping:

I was having a look at your article about finding the last row: I've always thought I was alone in considering this a conundrum! I've known a number of approaches - none totally satisfactory - and, as you indicate, I've always produced an acceptable method for the particular circumstance.

This is one of those questions that a human is able to answer very quickly - and revise the answer if asked to find "the last row of the worksheet, the last row within this area or table", etc. - yet is very hard to to describe programmatically.

Regards, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.