Help with VBA code for adding new rows

SirSquiddly

New Member
Joined
Jun 26, 2018
Messages
40
I have a button that adds 'x' amount of rows when desired number is entered. I feel this VBA is close and need help adjusting it. My data starts on row 5 and would like new blank (but containing formulas) added above the existing table. E,g,. pushes rows down.

A run-error '1004' pops up saying "this won't work because it would move cells in a table on your worksheet. I click debug and it highlights "Rng.Insert Shift:=x1Down"
Any help hugely appreciated
OFF HIREOFF HIRE-0

<tbody>
</tbody>

<tbody>
</tbody>



Code:


Sub AddRows()


Const BaseRow As Long = 5 ' modify to suit


Dim x As String ' InputBox returns text if 'Type' isn't specified
Dim Rng As Range
Dim R As Long


x = InputBox("How many rows would you like to add?", "Insert Rows")
If x = "" Then Exit Sub
R = BaseRow + CInt(x) - 1


Rows(BaseRow).Copy 'Copy BaseRow
'specify range to insert new cells
Set Rng = Range(Cells(BaseRow, 1), Cells(R, 1))
Rng.Insert Shift:=x1Down


' insert the new rows BEFORE BaseRow
' to insert below BaseRow use Rng.Offset(BaseRow - R)
Set Rng = Rng.Offset(BaseRow - R - 1).Resize(Rng.Rows.Count, ActiveSheet.UsedRange.Columns.Count)
Rng.Select
On Error Resume Next
Rng.SpecialCells(xlCellTypeConstants).ClearContents
Application.CutCopyMode = False '
End Sub
 
Last edited:
I just created a new workbook with a table and it appears to be working. Thanks for everyones help. Life savers!
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
False alarm.

It works when my table does not have headers. When the headers are re-entered the same message as before occurs. Unfortunately, I require the table to be sorted by A-Z, etc. Any ideas on this?
 
Upvote 0

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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