Insert rows at every nth row

Xrull

Board Regular
Joined
Dec 26, 2008
Messages
73
Hello,
I posted this in the wrong forum (General Excel Discussion & Other Questions)
I need to insert a row every 19th row after A2
I tried using this code:
Code:
Sub addblankrowevery19()
For i = Cells(Rows.Count, "a").End(xlUp).Row To 20 Step -19
Rows(i).Insert
Next i
End Sub
But it didn't work.
Can someone guide me on how to get it to work, or a better solution?
Thanks,
Xrull
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> addblankrowevery19()<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">For</SPAN> i = Cells(Rows.Count, "a").End(xlUp).Row <SPAN style="color:#00007F">To</SPAN> 20 <SPAN style="color:#00007F">Step</SPAN> -19<br>        Rows(i & ":" & i).Insert xlShiftDown<br>    <SPAN style="color:#00007F">Next</SPAN> i<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Perhaps:

Code:
Sub addblankrowevery19()
    Dim i As Long
    
    For i = Cells(Rows.Count, "a").End(xlUp).Row To 2 Step -19
        Rows(i).Insert
    Next i
    
End Sub

Although I do see problems with it in things evening out (unless you have rows that are a multiple of 19).

HTH,

Note: I deleted the errant post. ;)
 
Upvote 0
Smitty,
It worked, but is it possible to insert from the top down, instead of from the bottom up?
Xrull
 
Last edited:
Upvote 0
This seems to work:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> addblankrowevery19()<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lCnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">For</SPAN> i = 19 <SPAN style="color:#00007F">To</SPAN> Cells(Rows.Count, "A").End(xlUp).Row <SPAN style="color:#00007F">Step</SPAN> 19<br>        lCnt = lCnt + 1<br>        Rows(i + lCnt & ":" & i + lCnt).Insert xlShiftDown<br>    <SPAN style="color:#00007F">Next</SPAN> i<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Oops. Add in screenupdating to speed it up.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>    <br><SPAN style="color:#00007F">Sub</SPAN> addblankrowevery19()<br><SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lCnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 19 <SPAN style="color:#00007F">To</SPAN> Cells(Rows.Count, "A").End(xlUp).Row <SPAN style="color:#00007F">Step</SPAN> 19<br>        lCnt = lCnt + 1<br>        Rows(i + lCnt & ":" & i + lCnt).Insert xlShiftDown<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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