PDA

View Full Version : Copy Range To Array



ChloeRadshaw
11-03-2008, 07:25 AM
Hi All,

I ve managed to sync a range to an array by doing the following:
1) Dim arr As Variant (Notice no brackets!!!)
2) Set arr = rangeResults


This works and I can select the rangeResults range AND query values in the range

HOWEVER I CANNOT do UBound on the array so I cannot traverse it

Does anyone know whether this is possible?

I ve been trying to figure out for a while how to get a range into an array so I can then process it easily

Any help much appreciated

ChloeRadshaw
11-03-2008, 07:28 AM
I should add that this is a 2D array that has been read.

I can do array(0,1) and print out to the console - However I cannot do UBound(array, 0) or UBound(array, 1)

Bob Phillips
11-03-2008, 08:20 AM
The array dimensions are integer values, 1,2 etc, not ever 0



Dim arr As Variant

arr = rngResults
MsgBox UBound(arr, 1)
MsgBox UBound(arr, 2)

Bob Phillips
11-03-2008, 08:22 AM
BTW, all range arrays are 2D, even a single row/column.

ChloeRadshaw
11-03-2008, 10:35 AM
I guess I did a bad job explaining this.

If you do:
Dim myArray As Variant
myArray = range("A1:B2")

UBound(myArray, 1) - THIS THROWS AN ERROR WHICH I AM DESPERATE TO UNDERSTAND

What is really weird is I can do this:
myArray(1,1) but I cannot do UBound(myArray, 1)

Bob Phillips
11-03-2008, 10:59 AM
It depends upon what you are trying to do to it. This works fine



Dim myArray As Variant
myArray = Range("A1:B2")

MsgBox UBound(myArray, 1)

MsgBox myArray(1, 1)