不应用公式到最后一行

我想添加最后一行,然后把公式放在最后一行的特定单元格中。 当我运行我所拥有的东西时,它会将公式添加到旧的最后一行,而不是新创build的那一行。 你能帮忙吗?

Sub AddDataRow() Dim tableName As String Dim sheet As Worksheet Dim table As ListObject Dim lastRow As Range tableName = ActiveSheet.ListObjects(1) Set sheet = Range(tableName).Parent Set table = sheet.ListObjects.Item(tableName) If table.ListRows.Count > 0 Then Set lastRow = table.ListRows(table.ListRows.Count).Range If Application.CountBlank(lastRow) < lastRow.Columns.Count Then table.ListRows.Add End If End If lastRow.Columns("L").Formula = "=A1+B1" End Sub 

如果添加一行,则最后一行向下移动一行。

您需要在lastRow.Columns("L").Formula = "=A1+B1"之前再次确定最后一行。

在上面的行之前添加另一个Set lastRow = table.ListRows(table.ListRows.Count).Range