ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Index is not in range (https://www.excelbanter.com/excel-programming/425564-index-not-range.html)

leo[_4_]

Index is not in range
 
Hi
I have code:
Public Sub Main()

Dim P(1 To 2, 1 To 2) As Variant
Dim X(1 To 2, 1 To 2) As Variant
Dim r() As Variant

P(1, 1) = 3
X(1, 1) = 4
r = Test(P, X)

End Sub

Function Test(ByRef Arra1y As Variant, ByRef Arra2y As Variant) As
Variant

Dim ResultArray(1 to 2,1 to 2) As Variant


ResultArray(1)(1) = Arra2y(1)(1) + Arra1y(1)(1)

End Function

I have always error:Index is not in range. How can I return array in
this example?

Regards

Dave Peterson

Index is not in range
 
I'm not sure I understand, but maybe...

Option Explicit
Public Sub Main()
Dim P(1 To 2, 1 To 2) As Variant
Dim X(1 To 2, 1 To 2) As Variant
Dim r As Variant

P(1, 1) = 3
X(1, 1) = 4
r = Test(P, X)
End Sub
Function Test(ByRef Arra1y As Variant, ByRef Arra2y As Variant) As Variant
Dim ResultArray(1 To 2, 1 To 2) As Variant
ResultArray(1, 1) = Arra2y(1, 1) + Arra1y(1, 1)
End Function



leo wrote:

Hi
I have code:
Public Sub Main()

Dim P(1 To 2, 1 To 2) As Variant
Dim X(1 To 2, 1 To 2) As Variant
Dim r() As Variant

P(1, 1) = 3
X(1, 1) = 4
r = Test(P, X)

End Sub

Function Test(ByRef Arra1y As Variant, ByRef Arra2y As Variant) As
Variant

Dim ResultArray(1 to 2,1 to 2) As Variant

ResultArray(1)(1) = Arra2y(1)(1) + Arra1y(1)(1)

End Function

I have always error:Index is not in range. How can I return array in
this example?

Regards


--

Dave Peterson

Lars-Åke Aspelin[_2_]

Index is not in range
 
On Sat, 14 Mar 2009 11:02:41 -0700 (PDT), leo
wrote:

Hi
I have code:
Public Sub Main()

Dim P(1 To 2, 1 To 2) As Variant
Dim X(1 To 2, 1 To 2) As Variant
Dim r() As Variant

P(1, 1) = 3
X(1, 1) = 4
r = Test(P, X)

End Sub

Function Test(ByRef Arra1y As Variant, ByRef Arra2y As Variant) As
Variant

Dim ResultArray(1 to 2,1 to 2) As Variant


ResultArray(1)(1) = Arra2y(1)(1) + Arra1y(1)(1)

End Function

I have always error:Index is not in range. How can I return array in
this example?

Regards



Try changing (1)(1) to (1,1) in three places

and add

Test = ResultArray

at the end of you function, just before End Function

Hope this helps / Lars-Åke


All times are GMT +1. The time now is 09:45 AM.

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