Macro to insert a new blank line at the end of a table maintaining formats and formulas of the last one

robyrux

New Member
Joined
Oct 29, 2020
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi all, can somebody please post me a VBA macro to insert a new blank line at the end of a table that maintains the formats and the formulas of the last line?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi.

See if this works:
VBA Code:
Sub addRow()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Set tbl = ws.ListObjects("Table1") ' Change "Table1" for your table name

'add a row at the end of the table
tbl.ListRows.Add

End Sub
 
Upvote 0
a couple of informative sites for when dealing with tables
 
Upvote 0
Tks!
It does not work because Table1 is not defined. My table is actually a list of records I am tracking in a worksheet. What I need to do is to be able to add a new blank line at the end with the same format and formulas as the last row and continue editing from there. I could do it with a copy & paste but it is clumsy and not good for others to use as well.

Thanks so much
 
Upvote 0
Check this:
VBA Code:
Sub addRow()
Dim lastRow As Long
Dim col As Long
Dim row As Long
Dim lastCol As Long
Dim i As Long

lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row

Range("A" & lastRow).EntireRow.Offset(1, 0).Insert

col = 5000

row = 1

lastCol = ActiveSheet.Cells(row, col).End(xlToLeft).Column

Range("A" & lastRow, Cells(lastRow, lastCol)).Copy
Range("A" & lastRow + 1).PasteSpecial xlFormats

For i = 1 To lastCol

If Cells(2, i).HasFormula = True Then
    
    Cells(2, i).Select
    Selection.AutoFill Destination:=Range(Cells(2, i), Cells(lastRow + 1, i))
    
End If

Next i

End Sub
 
Upvote 0
Another option
VBA Code:
Sub robyrux()
   With Range("A" & Rows.Count).End(xlUp)
      .EntireRow.Resize(2).FillDown
      On Error Resume Next
      .Offset(1).EntireRow.SpecialCells(xlConstants).ClearContents
      On Error GoTo 0
   End With
End Sub
 
Upvote 0
Another option
VBA Code:
Sub robyrux()
   With Range("A" & Rows.Count).End(xlUp)
      .EntireRow.Resize(2).FillDown
      On Error Resume Next
      .Offset(1).EntireRow.SpecialCells(xlConstants).ClearContents
      On Error GoTo 0
   End With
End Sub
Thank you so much! It works!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top