An implementation of Visual Basic that is built into Microsoft products.
Hi @CamaDragon ,
You can use VBA's Application.ConvertFormula method to copy the formula from the cell 36 rows above the active cell while incrementing its relative row references by 1.
For example, if B164 contains = Sheet2!A20, running the macro with B200 as the active cell will place=Sheet2!A21 in B200.
You can use the following VBA code:
Sub CopyFormulaAndIncrementReference()
Dim target As Range
Dim source As Range
Dim r1c1Formula As Variant
Set target = ActiveCell
If target.Row <= 36 Then
MsgBox"The active cell must be below row 36."
Exit Sub
End If
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
Few additional things for you to considerate is that: the macro uses ActiveCell to determine the target, so make sure you select intended destination cell before running it. And this approach increments all relative row references in the formula. Absolute row references, such as $A$20 or A$20, remain unchanged.- If the source formula is: =Sheet!A20+$B$30
- The resulting formula will be: =Sheet2!A21+$B$30
Here, A20 changes to A21, while $B$30 remains unchanged because its row and column are absolute references. I f your formula contains multiple references, and you only want to increment a specific one, the logic would need to be adjusted accordingly.
If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation.
Thank you.