3 dimensional array

S

Sandy

I am trying to self learn vba and at this point I am dabbling with arrays. I
have a command button on sheet Array1 called cmd3dArray, also on this sheet
I have data a, b, c, etc in cells A1:E5. In the next sheet Array2 I have
data in A1:E5 namely a1, b1, c1 etc and similarly in sheet Array3, a2, b2
etc. I am trying to collect the data from the three sheets into a
3-dimensional array - I'm struggling - any offers.
Sandy
 
D

Dave Peterson

One way:

Option Explicit
Sub testme()
Dim wCtr As Long
Dim iCol As Long
Dim iRow As Long
Dim myArray(1 To 5, 1 To 5, 1 To 3) As Variant
'rows x cols x sheets

For wCtr = 1 To 3
For iRow = 1 To 5
For iCol = 1 To 5
myArray(iRow, iCol, wCtr) _
= Worksheets("array" & wCtr).Cells(iRow, iCol).value
Next iCol
Next iRow
Next wCtr

End Sub
 
D

Dennis W. Bulgrien

I don't understand why you want a 3 dimentioned array. Perhaps a two
dimentioned array with the first index to indicate which of the 3 sheets:

Dim c, i
Dim MyArray(3, 25)

i = 0
For Each c In Worksheets("Array1").Range("A1:E5")
MyArray(1, i) = c
i = i + 1
Next c
i = 0
For Each c In Worksheets("Array2").Range("A1:E5")
MyArray(2, i) = c
i = i + 1
Next c
i = 0
For Each c In Worksheets("Array3").Range("A1:E5")
MyArray(2, i) = c
i = i + 1
Next c

....I have data a, b, c, etc in cells A1:E5. In the next sheet Array2 I have
data in A1:E5 namely a1, b1, c1 etc and similarly in sheet Array3, a2, b2
etc. I am trying to collect the data from the three sheets into a
3-dimensional array - I'm struggling - any offers.
Sandy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top