具有可变固定单元格的R1C1公式格式

时间:2018-12-07 20:59:09

标签: excel excel-vba

我正在研究自动插入公式以执行计算的代码。这些形式变量的插入范围不断变化,我需要使用R1C1公式引用。我无法弄清楚如何在第二个For Each R1C1公式中将“ g”设置为绝对单元格引用。任何帮助深表感谢。

Sub FindRow1()
Dim t As Range
Dim c As Range
Dim d As Range
Dim e As Range
Dim f As Range
Dim g As Range


With Worksheets("Recap Sheet").Cells
Set t = .Find("Year of Tax Return", After:=.Range("A1"), 
LookIn:=xlValues).Cells
Set c = .Find("12. Total Gross Annual Cash Flow", After:=.Range("A1"), 
LookIn:=xlValues).Cells
Set d = .Find("15. Total Annual Cash Flow Available to Service Debt", 
After:=.Range("A1"), LookIn:=xlValues)
Set e = Range(t.Offset(1, 0), c.Offset(-1, 0))
Set f = Range(c.Offset(1, 0), d.Offset(-1, 0))
Set g = c.Offset(0, 9).Cells


For Each cell In e
If cell.Value <> "" Then
cell.Offset(0, 9).FormulaR1C1 = 
"=average(R[0]C[-2],R[0]C[-4],R[0]C[-6],R[0]C[-8])"
End If
Next

For Each cell In e
If cell.Value <> "" Then
'& g & in the formula below does not equal the cell location set above
cell.Offset(0, 10).FormulaR1C1 = "=R[0]C[-1]/" & g & ""
End If
Next

For Each cell In f
If cell.Value <> "" Then
cell.Offset(0, 10).FormulaR1C1 = 
"=average(R[0]C[-2],R[0]C[-4],R[0]C[-6],R[0]C[-8])"
End If
Next

For Each cell In f
If cell.Value <> "" Then
cell.Offset(0, 9).FormulaR1C1 = "=R[-2]C[0]*(R[0]C[1]/100)"
End If
Next

End With

End Sub

1 个答案:

答案 0 :(得分:3)

cell.Offset(0, 10).FormulaR1C1 = "=R[0]C[-1]/" & g.Address(True, True, xlR1C1)