所以我想知道如何更新范围内的公式(E7:BE7),在我的工作表中,我有多个表,所以我无法使用lastRow更新它。
对于E7:BE7范围内的每个单元格,我都有以下公式:
在使用脚本向表中添加新条目之后,我想更新公式,以使最后一行加1。因此,我要让该范围内的所有公式在新输入后都变成这样:
这是我的脚本的一个片段,其中添加了一个新条目,我想自动更新这些公式,但是我对vba还是陌生的,有点挣扎,任何帮助将不胜感激。
With Target
Dim cell As Range
Dim calendar_header As Range
Set calendar_header = Range("E7:BE7")
' this is the part that will insert a new row on my target and copy the information i need in the row
.Offset(0).EntireRow.Insert Shift:=xlDown
.Offset(-2).EntireRow.Copy
.Offset(-1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
Cells(.Row - 1, "A").Value = Cells(.Row - 2, "A").Value
Cells(.Row - 1, "B").Value = Cells(.Row - 2, "B").Value
Cells(.Row - 2, "E").Copy
Cells(.Row - 1, "E").PasteSpecial
Application.CutCopyMode = False
' This is the loop that's supposed to make my updates on the formulas
For Each cell In calendar_header
' Im struggling with this part.
Next cell
End With
答案 0 :(得分:1)
您不需要循环即可更新公式。假设您的Target
是最后一行,您可以这样做:calendar_header.Formula = "-SUM(E$8:E$" & Target.Row & ")"
。这将更新您范围内的公式
注意:还要考虑如果Target
的单元格超过1个会发生什么情况
答案 1 :(得分:0)
这可能会有所帮助:
Sub AutoSum()
For i = 5 To 57
Last_Row = Application.ActiveSheet.Cells(Rows.Count, i).End(xlUp).Row + 1
Cells(Last_Row, i).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-" & Last_Row - 1 & "]C:R[-1]C)"
Next i
End Sub
注释::我的列号是5(对于E是5,对于BE是57),因此从5到57。