• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Insert New Row & Carry Forward Table Formatting (VBA)

cmkarnes

Member
Hello, I have an excel Table that I've made a command button to insert row below the row containing the active cell.

It works fine if you are in rows within the table - my problem is if you are in the last row of the table, want to insert a row below that, it doesn't carry forward the formulas, nor the formatting of the table itself. I am not sure how to incorporate that into the vba. In case a range would need to be specified, the table starts on A2 and goes through row W. Thank you for your assistance.

Code:
Private Sub CommandButton2_Click()
Sheets("Main Table").Select
'Insert row below active cell
ActiveCell.Offset(1).EntireRow.Insert
End Sub
 
Something like below code. Adjust sheet and table name as needed.

Code:
Sub AddTableRow()

Dim mTable As ListObject
Set mTable = ActiveWorkbook.Sheets("Sheet2").ListObjects.Item("Table1")
mTable.ListRows.Add
End Sub
 
Thank you so much-does exactly what I need for this project!! I noticed the vba goes straight to the last row and inserts a new row after, which works perfectly for my scenario - but for future reference, is there something I should modify if I wanted to make this code effective for the entire table? Or would I stick with my original vba for inserting new rows from within the table? Thanks!
 
Back
Top