Insert Row at Bottom of Table Maintain Formulas and Format Using VBA Button

gplans

New Member
Joined
May 30, 2019
Messages
41
I would like to know how you can use a macro to insert a row at the very bottom of your table and also maintain the formatting and formulas from the table.

I used this code but it does not copy the formatting or the formulas from the line above.

I use this technique from the link in this video.

https://www.youtube.com/watch?v=9JTLJ1XjOJc

Range("A" & Rows.Count).End(xlUp).Select
ActiveCell.EntireRow.Insert

Any help Thanks
 

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".
Try

Code:
Sub MM1()
    With Rows(Cells(Rows.Count, "A").End(xlUp).Row)
         .Insert
         .Copy
        .Offset(-1).EntireRow.PasteSpecial Paste:=xlPasteFormats
        .Offset(-1).EntireRow.PasteSpecial Paste:=xlPasteFormulas
    End With
Application.CutCopyMode = False
End Sub
 
Last edited:
Upvote 0
Try

Code:
Sub MM1()
    With Rows(Cells(Rows.Count, "A").End(xlUp).Row)
         .Insert
         .Copy
        .Offset(-1).EntireRow.PasteSpecial Paste:=xlPasteFormats
        .Offset(-1).EntireRow.PasteSpecial Paste:=xlPasteFormulas
    End With
Application.CutCopyMode = False
End Sub

Thanks Michael for the quick response.

It is very close but when it copies the format and formulas it copies the very bottom line that contains the sum amount. Also is it possible to have the OBS CHEOPS # add in sequence OBSM100034, OBSM100035, OBSM100036 when I add the new row.

I have attached the file via the link below.

https://filebin.net/xjxa2z8ekzn13roo

Image
https://filebin.net/q4bv2dh9hp2pjg7h
 
Upvote 0
Maybe this then

Code:
Sub MM2()
Dim myRow As Long
    myRow = Cells(Rows.Count, "A").End(xlUp).Row
    With Rows(myRow)
        .Copy
        .Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    End With
Range("A" & myRow + 1).Value = "OBSM" & Right(Range("A" & myRow).Value, 6) + 1
Application.CutCopyMode = False
End Sub
 
Upvote 0
Maybe this then

Code:
Sub MM2()
Dim myRow As Long
    myRow = Cells(Rows.Count, "A").End(xlUp).Row
    With Rows(myRow)
        .Copy
        .Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    End With
Range("A" & myRow + 1).Value = "OBSM" & Right(Range("A" & myRow).Value, 6) + 1
Application.CutCopyMode = False
End Sub

I get a Run-Time error '1004'
Application-defined or object-defined error

Were you able to get it to work on the one I uploaded to https://filebin.net/xjxa2z8ekzn13roo

Thanks Again for looking.
 
Upvote 0
I don't download zip files !!
Someone else may jump in and download the file to give you assistance.
If it's an actual Excel table the code will have to be changed to reflect the name and list objects of the table.
 
Upvote 0
If you want to add a row to a table you can use ListRows.Add.
Code:
Dim tblRow As ListRow

    Set tblRow = Sheets("OBS Main Data").ListObjects("Table5").ListRows.Add

    tblRow.Range.Offset(-1).Copy

    tblRow.Range.PasteSpecial xlPasteFormulasAndNumberFormats

    Application.CutCopyMode = False
 
Upvote 0
If you want to add a row to a table you can use ListRows.Add.
Code:
Dim tblRow As ListRow

    Set tblRow = Sheets("OBS Main Data").ListObjects("Table5").ListRows.Add

    tblRow.Range.Offset(-1).Copy

    tblRow.Range.PasteSpecial xlPasteFormulasAndNumberFormats

    Application.CutCopyMode = False

No that still has not worked. It copes the very last line which has the sum total formulas in it. It should copy the second from the last row and insert it in the second from the last row. This means there will will always have a total at the bottom.


1. Format & Formulas
2. Format & Formulas
3. Format & Formulas
4. Totals

New line would go in between 3 & 4

Also it would be good when adding a new line the numbering would be added in sequential order. OBSM100034, OBSM100035, OBSM100036

Link to the spreadsheet that is not zipped:

https://www.dropbox.com/s/3zptvki2g5p91qr/MASTER SPREADSHEET.xlsm?dl=0

Thanks for your help.
 
Upvote 0
Maybe this then


Code:
Private Sub CommandButton1_Click()
Dim myRow As Long, tblRow As ListRow
    myRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set tblRow = Sheets("OBS Main Data").ListObjects("Table5").ListRows.Add(myRow - 2)
    tblRow.Range.Offset(-1).Copy
    tblRow.Range.PasteSpecial xlPasteFormulasAndNumberFormats
    Application.CutCopyMode = False
    With Range("A" & myRow)
        .Value = "OBSM" & Right(Range("A" & myRow).Value, 6) + 1
        .HorizontalAlignment = xlCenter
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
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