您好,您知道如何将r1 r2 r3编号设置为R1C1公式,以确保没有错误吗?感谢
Sub ReapplyFormula()
Dim r1, r2, r3 As Integer
r1 = Range("Range1").row + 1 - Range("EndTable").row
r2 = Range("Range2").row + 1 - Range("EndTable").row
r3 = Range("Range3").row - Range("EndTable").row
For i = Range("Start").Column To Range("End").Column - 1
Cells(Range("End").row, i).Select
ActiveCell.FormulaR1C1 = _
"=IF(SUM(R[r1]C:R[-1]C) = SUM(R[r2]C:R[r3]C),0, SUM(R[r1]C:R[-1]C) - SUM(R[r2]C:R[r3]C))"
Next
End Sub
答案 0 :(得分:0)
你的公式应该是:
"=IF(SUM(R[" & r1 & "]C:R[-1]C) = _
SUM(R[" & r2 & "]C:R[" & r3 & "]C),0, _
SUM(R[" & r1 & "]C:R[-1]C) - _
SUM(R[" & r2 & "]C:R[" & r3 & "]C))"
如果要将变量添加到String
,则需要结束字符串"
使用&
添加到字符串,然后变量r1
再次添加{{ 1}}并继续&
String
答案 1 :(得分:0)
我不希望你想要相对行号,所以你的代码不应该使用[] - 它看起来像:
Cells(Range("End").row, i).FormulaR1C1 = _
"=IF(SUM(R" & r1 & "C:R[-1]C) = SUM(R" & r2 & "C:R" & r3 & "C),0, SUM(R" & r1 & "C:R[-1]C) - SUM(R" & r2 & "C:R" & r3 & "C))"