VBA table resize is replacing the content below

2019-08-20 17:21发布

I am trying to filldown the formulas in the 1st row of my table to multiple rows bellow that, but it's replacing the data in the cells bellow that. Ideally it should everything else down not overwrite it.

Sub SelectTableBody()
Dim rTableData As Range
Dim i, last As String

last = 5
For i = 1 To last
With ThisWorkbook.Worksheets(1)
    Set rTableData = .ListObjects("Table1").DataBodyRange
    Set rTableData = rTableData _
      .Resize(i, rTableData.Columns.Count)
End With
Next

rTableData.FillDown
End Sub

Table before resizing

I also tried this code:

resizeSh.ListObjects(tablename).ListRows.Add AlwaysInsert:=True
resizeSh.ListObjects(tablename).DataBodyRange.FillDown

It's giving me error, that the code is attempting to shift cells in my table... Is there a way to change the 1st line to get the last entry in the table and then insert the whole row for all columns, not only the table?

标签: excel vba
1条回答
老娘就宠你
2楼-- · 2019-08-20 18:05

Maybe give this a try :

Sub SelectTableBody()
Dim rTableData As Range
Dim i, last As String

last = 5
For i = 1 To last
With ThisWorkbook.Worksheets(1)
    Set rTableData = ThisWorkbook.Worksheets(1).ListObjects("Tableau1").DataBodyRange _
      .Resize(i, ThisWorkbook.Worksheets(1).ListObjects("Tableau1").DataBodyRange.Columns.Count)
End With
Next

rTableData.FillDown
End Sub

enter image description here

After Macro's execution will give :

enter image description here

查看更多
登录 后发表回答