Thanks for pointing out my mistake, I see you have changed integer to variant as Application.index method returns a variant array. This is why an API would be helpful as you would be able to see the return type of methods etc
Now I want to use this to print out my 2D array, this work fine where i declare my array with even dimension but if I was to change my array declaration to the below it gives a runtime error??
Dim arr(1 To 10, 1 To 5) As Integer
Public Sub test()
print2DArray create2DArray()
End Sub
'This takes a string and returns a string reprenstation delimited by commas e.g [1,2,3]
Private Function arrayToString(ByRef arr() As Variant) As String
Dim i As Integer
Dim str As String
str = "["
For i = LBound(arr) To UBound(arr)
str = str & Format(Val(arr(i)), "000") & ","
Next i
arrayToString = Left(str, Len(str) - 1) + "]"
End Function
'creates and returns a 2d integer array
Private Function create2DArray() As Integer()
Dim arr(1 To 10, 1 To 10) As Integer
Dim i As Integer
Dim j As Integer
For i = LBound(arr) To UBound(arr)
For j = LBound(arr) To UBound(arr)
arr(i, j) = i * j
Next j
Next i
create2DArray = arr
End Function
'This used the method arrayToString to print out 2D arrays
Private Function print2DArray(ByRef arr() As Integer)
Dim i As Integer
Dim varr() As Variant
For i = LBound(arr) To UBound(arr)
varr = Application.Index(arr, i, 0)
Debug.Print arrayToString(varr)
Next i
End Function
Thanks again
Chris