I'm new to Variant Arrays and everyone says they are a game-changer so I'm trying to get this to work! Spent a couple days reading messages and just can't seem to grasp what I need to do.
Here the code I'm trying to work with but I can't even get past the subscript out of range for the first part of the loop...
Sub Array_Formula_Practice()
'Variants
Dim ArrTopFormulas As Variant, ArrData As Variant, ArrBodyFormulas As Variant
Dim R As Long, C As Long, i As Long
'LastRow/Column
Dim LastRow As Long, LastCol As Long
With Sheets("Sheet1")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
ArrTopFormulas = .Range(.Cells(5, 2), .Cells(5, LastCol)).Formula
ArrBodyFormulas = .Range(.Cells(10, 2), .Cells(LastRow, LastCol))
For C = 2 To LastCol
For R = 10 To LastRow
If Left(ArrTopFormulas(5, C), 1) = "=" Then ArrBodyFormulas(R, C) = ArrTopFormulas(5, C)
Next R
Next C
' Now put ArrFormulas back where it came from
.Range(.Cells(10, 2), .Cells(LastRow, LastCol)).Formula = ArrBodyFormulas
End With
End Sub
- Row 5 has formulas (though some cells are blank)
- Starting on Row 10 is where I'd like to copy the formulas to (even row 5 has a formula). I need them to be relative references - see attached sheet for example
- All formulas refer back to column 1 (this is maybe confusing but there is an API connection that drives off the value in column 1 - I tried to simplify this).
Here the code I'm trying to work with but I can't even get past the subscript out of range for the first part of the loop...
Sub Array_Formula_Practice()
'Variants
Dim ArrTopFormulas As Variant, ArrData As Variant, ArrBodyFormulas As Variant
Dim R As Long, C As Long, i As Long
'LastRow/Column
Dim LastRow As Long, LastCol As Long
With Sheets("Sheet1")
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
ArrTopFormulas = .Range(.Cells(5, 2), .Cells(5, LastCol)).Formula
ArrBodyFormulas = .Range(.Cells(10, 2), .Cells(LastRow, LastCol))
For C = 2 To LastCol
For R = 10 To LastRow
If Left(ArrTopFormulas(5, C), 1) = "=" Then ArrBodyFormulas(R, C) = ArrTopFormulas(5, C)
Next R
Next C
' Now put ArrFormulas back where it came from
.Range(.Cells(10, 2), .Cells(LastRow, LastCol)).Formula = ArrBodyFormulas
End With
End Sub