【问题标题】:Wont apply formula to last row不会将公式应用于最后一行
【发布时间】:2017-04-20 21:22:12
【问题描述】:

我正在尝试添加最后一行,然后将公式放在最后一行的特定单元格中。当我运行我所拥有的内容时,它会将公式添加到 OLD 最后一行,而不是我新创建的那个。你能帮忙吗?

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

【问题讨论】:

    标签: excel excel-formula insert row vba


    【解决方案1】:

    如果您添加一行,那么您的最后一行会下移一行。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多