I'm trying to get the values of a 2D array onto a worksheet, starting at a certain cell.
For example, in cell A1
, I type the formula =testArray2Sheet(D7)
The expected outcome would be that the values appear on the sheet, the first value being in cell D7
and spanning 18 rows down and 3 columns across.
My current code just stops executing at this line: targetRange.Value = arr
and exits without throwing a warning or an error.
Cell A1
just says #VALUE
.
I have no idea why though...
Function testArray2Sheet(firstCell As range)
Dim ret As Boolean 'dummy return value
Dim targetRange As range
Dim lastCell As range
Dim arr As Variant
Dim rows, cols As Integer
Dim i, j As Integer
'Determine size of array
rows = 18
cols = 3
'Make sure the array has the new dimensions
ReDim arr(1 To rows, 1 To cols)
'Fill the array with values
For i = 1 To 18
For j = 1 To 3
arr(i, j) = i * j
Next
Next
'firstCell is the top-left corner of the targetRange
'Now determine the bottom-right corner of the targetRange
Set lastCell = firstCell.Offset(rows, cols)
'Create the targetRange
Set targetRange = range(firstCell, lastCell)
'Put the values of the array to the targetRange
'This should me the values appear in the worksheet
targetRange.Value = arr
'Return a dummy value, because a function needs to return something
testArray2Sheet = ret
End Function