VBA Code to insert rows receiving an error

crittenberry

New Member
Joined
Mar 29, 2016
Messages
8
Hello,

I am using a macro that imports data from a third party source via a txt file. The data is then split into new worksheets based on an ID number. (The number of spreadsheets created will vary per run). This part of the macro runs perfectly. The issue I am having is when the macro tries to add new rows. This is what I am currently using for that part:


Invoice = Sheets.Count
For x = 3 To Invoice
Sheets(x).Select
Range("A1").Select
Rows("1:5").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Next x




The first new worksheet goes fine; however, when it goes to the next sheet the macro returns the following error:

"Microsoft Excel can't insert new cells because it would push non-empty cells off the end of the worksheet. These cells might appear empty but have blank values, some formatting, or a formula. Delete enough rows or columns to make room for what you want to insert and then try again.


The rows are being added for formatting.

Any help would be greatly appreciated.


 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If you select one the sheets that causing a problem & run this
Code:
Sub chk()
MsgBox ActiveSheet.UsedRange.Rows.Count
End Sub
What does the message box say?
 
Upvote 0
The one I ran it on says 745.

I also just noticed that if I manually try to insert rows after the error, the option is "insert copied cells" instead of simply "insert"
 
Upvote 0
In that case try
Code:
Dim x As Long
Application.CutCopyMode = False
For x = 3 To Worksheets.Count
   With Sheets(x)
      .Rows("1:5").Insert
   End With
Next x
 
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

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