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



我正在编写一个代码,该代码可以自动插入公式来执行计算。这些公式插入的范围不断变化,我需要使用R1C1公式引用。我不知道如何将"g"设置为第二个For Each R1C1公式中的绝对单元格引用。非常感谢您的帮助。

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
cell.Offset(0, 10).FormulaR1C1 = "=R[0]C[-1]/" & g.Address(True, True, xlR1C1)

最新更新