Home » excel » excel – FormulaR1C1 Dynamic Reference

excel – FormulaR1C1 Dynamic Reference

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m having a problem referencing data from Sheet 2 back to Sheet 1 (which is a summary tab).

I want to reference the last row of column A which contains the summation back to cell A1 of Sheet 1, but the problem is that the number of rows differ (as this is dynamic) on every report.

How can i make the following formula dynamic?

ActiveCell.FormulaR1C1 = "=Sheet2!R[-23]C"
How to&Answers:

Like this?

Option Explicit
Public Sub TEST()
    With Worksheets("Sheet2")
        Worksheets("Sheet1").Range("B12").Formula = "=Sheet2!" & .Range("H" & .Cells(.Rows.count, "H").End(xlUp).Row).Address
    End With
End Sub

Sheet2! is to make sure the formula references sheet2 when applied in the cell.

Then

.Range("H" & .Cells(.Rows.count, "H").End(xlUp).Row

finds the last row in column H of sheet 2 by coming up from the bottom of the sheet.

Answer:

Pass the number of rows as a variable like this one:

Sub TestMe()

    Dim numberOfRows As Long

    numberOfRows = -12        
    ActiveCell.FormulaR1C1 = "=Sheet2!R[" & numberOfRows & "]C"

End Sub