An implementation of Visual Basic that is built into Microsoft products.
Translate the formula from 36 rows above as though it moved down only one row, then assign that translated formula to the active cell.
Sub CopyFormulaAndIncrementReference()
Dim target As Range
Dim source As Range
Dim r1c1Formula As Variant
Set target = ActiveCell
If target.Row <= 36 Then Exit Sub
Set source = target.Offset(-36, 0)
r1c1Formula = Application.ConvertFormula( _
source.Formula, xlA1, xlR1C1, xlRelative, source)
target.Formula = Application.ConvertFormula( _
r1c1Formula, xlR1C1, xlA1, xlRelative, source.Offset(1, 0))
End Sub
For your example:
- Active cell:
Sheet1!B200 - Source formula cell:
Sheet1!B164 - Formula in
B164:=Sheet2!A20 - Result placed in
B200:=Sheet2!A21
Offset(-36, 0) identifies the cell 36 rows above without selecting it.^1^ ConvertFormula first represents the references relative to B164, then reconstructs them relative to B165; this advances relative row references by exactly one rather than by the 36-row distance to B200.^2^
Important limitations
- References with an absolute row, such as
=Sheet2!A$20or=Sheet2!$A$20, will remain on row 20. Use a relative reference such asA20if it must increment. - Every relative reference in the formula is translated one row down, not only the first cross-sheet reference.
-
Application.ConvertFormulahas a 255-character formula limit.^2^
References