ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Returning an array from a multi-dimensional array (https://www.excelbanter.com/excel-programming/380412-returning-array-multi-dimensional-array.html)

Chris

Returning an array from a multi-dimensional array
 
Is there a way to return a one-dimensional array from a
multi-dimensional array?
For example, I have the following two-dimensional array;

A B C D E F G
H J K L M N O
P Q R S T U V

Dim array1(2, 7) As String

But I want to get, say, the first row but when I enter array1(0) VBA
produces an error.

In Java, the above commnad, array1(0) will return an array containing
the items in the first row,
but in VBA, I couldn't figure out how to do it.

Do I have to create a one-dimensional array with variant type, then
enter one-dimensional arrays containing each row?

Thanks.


Dave Peterson

Returning an array from a multi-dimensional array
 
Maybe this example will help:

Option Explicit
Sub testme01()

Dim myArr As Variant
Dim myArrRow As Variant
Dim myArrCol As Variant

With ActiveSheet
myArr = .Range("a1:g3").Value '3 row x 7 col
With Application
'just a sample: 2 row of array
myArrRow = .Index(myArr, 2)

'3rd column of original array
myArrCol = .Transpose(.Index(myArr, 0, 3))
End With
End With

End Sub

I included both row and column just because.

And .transpose() is limited to 5417 elements (IIRC) in xl2k and below.



Chris wrote:

Is there a way to return a one-dimensional array from a
multi-dimensional array?
For example, I have the following two-dimensional array;

A B C D E F G
H J K L M N O
P Q R S T U V

Dim array1(2, 7) As String

But I want to get, say, the first row but when I enter array1(0) VBA
produces an error.

In Java, the above commnad, array1(0) will return an array containing
the items in the first row,
but in VBA, I couldn't figure out how to do it.

Do I have to create a one-dimensional array with variant type, then
enter one-dimensional arrays containing each row?

Thanks.


--

Dave Peterson

Alan Beban

Returning an array from a multi-dimensional array
 
Application.Index(array1,n,0) will return the nth row of array1

Alan Beban

Chris wrote:
Is there a way to return a one-dimensional array from a
multi-dimensional array?
For example, I have the following two-dimensional array;

A B C D E F G
H J K L M N O
P Q R S T U V

Dim array1(2, 7) As String

But I want to get, say, the first row but when I enter array1(0) VBA
produces an error.

In Java, the above commnad, array1(0) will return an array containing
the items in the first row,
but in VBA, I couldn't figure out how to do it.

Do I have to create a one-dimensional array with variant type, then
enter one-dimensional arrays containing each row?

Thanks.



All times are GMT +1. The time now is 03:10 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com