2017-04-20 6 views
1

最後の行を追加しようとしていますが、最後の行に特定のセルに数式を挿入しようとしています。私が持っているものを動かすと、私は新しく作ったものではなく、最後の行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 

答えて

0

行を追加すると、最後の行が1行下に移動します。

lastRow.Columns("L").Formula = "=A1+B1"の前にもう一度最後の行を決定する必要があります。

上記の行の前に別のSet lastRow = table.ListRows(table.ListRows.Count).Rangeを追加してください。

関連する問題