View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Reading data into a 2 dimensional array

Hi Keith,

I think that unlike normal arrays, if you test you will find that both
methods are 1 based by default. Try out the following examples.

Sub RngToArray()

Dim myArray As Variant
Dim Low1 As Long
Dim Upp1 As Long
Dim Low2 As Long
Dim Upp2 As Long
Dim i As Long
Dim j As Long

myArray = Sheets("Sheet1").Range("A1:D4")

'Lowest element in first dimension
Low1 = LBound(myArray, 1)

'Highest element in first dimension
Upp1 = UBound(myArray, 1)

'Lowest element in second dimension
Low2 = LBound(myArray, 2)

'Highest element in second dimension
Upp2 = UBound(myArray, 2)

MsgBox "Low1 = " & Low1 & vbCrLf & _
"Low2 = " & Low2 & vbCrLf & _
"Upp1 = " & Upp1 & vbCrLf & _
"Upp2 = " & Upp2

For i = Low1 To Upp1
For j = Low2 To Upp2
MsgBox myArray(i, j)
Next j
Next i

End Sub


Sub RngToArray2()

Dim myArray As Variant
Dim Low1 As Long
Dim Upp1 As Long
Dim i As Long

myArray = Sheets("Sheet1").Range("A1:A4")

'Lowest element in first dimension
Low1 = LBound(myArray, 1)

'Highest element in first dimension
Upp1 = UBound(myArray, 1)

MsgBox "Low1 = " & Low1 & vbCrLf & _
"Upp1 = " & Upp1

'Note because assigned to variant you must
'use the first dimension in the formula
For i = Low1 To Upp1
MsgBox myArray(i, 1)
Next i

'Alternative using 1 as the first element
'For i = 1 To Upp1
' MsgBox myArray(i, 1)
'Next i

End Sub

Sub RngTest()

Dim myRng As Range
Dim i As Long
Dim j As Long

Set myRng = Sheets("Sheet1").Range("A1:D4")
For i = 1 To 4
For j = 1 To 4
MsgBox myRng(i, j)
Next j
Next i


End Sub

--
Regards,

OssieMac