Add new Rows with VBA.

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
Hello,
I’m adding new / empty Rows (in this case 8463 Rows, starting at Row 21).
The first code I got using the Macro recorder. (It simply adds rows one after the other). I modified it to reduce the steps by putting a simple loop in. It works but takes ages (even if I turn the screen off with Application.screenupdating = False ).

Code:
Sub Insert8463RowsAtRow21ByInserting8463Rows()
    Rows("21:21").Select
    For i = 1 To 8463 Step 1
    Selection.Insert Shift:=xlDown
    Next i
End Sub 'Insert8463RowsAtRow21ByInserting8463Rows()




The Second Code I got as well from the recorder. (It shifts everything down to make a space equal in Rows to the number of Rows I want.) That works quite quickly so I’m Happy. (I took the line Range("A8483").Activate out because I couldn’t see that it did anything. I hope that was OK??)


Code:
Sub Insert8463RowsAtRow21ByMovingEverything8463Downwards()
    Rows("21:8483").Select
    Selection.Copy
    Rows("8484:8484").Select
    ActiveSheet.Paste
    Rows("21:8483").Select
    '      Range("A8483").Activate
    Application.CutCopyMode = False
    Selection.ClearContents
End Sub 'Insert8463RowsAtRow21ByMovingEverything8463Downwards()


But I am learning VBA and wanted to do it more professionally. I’ve tried a couple of hours to find a simple line that looks something like.

“ Rows(“21:21”) . Add .AddRows insert Range( 21 21 : 8483 8483 ).Add Rows.Insert( ) etc. etc. “

But I haven’t found it yet. It’s probably obvious to a Profi. – can anyone help
Thanks, Danke
Alan
Germany.
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
So in Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Value = 123 “ are we using the Range object or the Range Property of Worksheets("Sheet1") ... If it is the Range Property then it might explain why I need to initially select Cells(1 , 1) to avoid strange inconsistant errors sometimes.

We are using the Worksheet's Range property which returns a Range object (on that Worksheet). One reason that I can think of for needing to apply Cells(1, 1) to a Range object is that the Range's Value property can fail if the Range isn't a single cell (for example when testing it for equality).
 
Upvote 0
............Thanks for the link....... Configuring Help in VBA Editor 2010 ...." In the help page that opens (when you hit F1) in the bottom right corner can you see a button that probably says something like "Office Online" or some such value? Click this and change it to "Show Content From This Computer Only", then retry"

....That did the trick!!!!
 
Upvote 0

Forum statistics

Threads
1,223,631
Messages
6,173,464
Members
452,516
Latest member
archcalx

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