Adding rows to a table VBA

jlabrecque

New Member
Joined
Nov 7, 2018
Messages
14
So I have this code that works to add rows to a table.

Sub insert_rows()


Dim x As Integer
x = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)

Range("A" & Rows.Count).End(xlUp).Select
Range(ActiveCell, ActiveCell.Offset(x - 1, 0)).EntireRow.Insert Shift:=xlDown

End Sub


If you are using blue and white row 1 would be blue and row 2 white and row 3 blue again etc.

When the above code is used. The table loses its format as discussed above

I think the problem is occurring with how I am selecting the range.

Any advice is appreciated
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Are the rows definitely being added to the table?
 
Upvote 0
Does this work any better?
Code:
Sub insert_rows()
Dim tbl As ListObject
Dim rng As Range
Dim x As Long

    x = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)
    
    Set tbl = ActiveSheet.ListObjects("Table1")

    Set rng = tbl.ListRows(tbl.ListRows.Count).Range

    rng.Resize(x).Insert Shift:=xlDown

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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