VBA insert blank row until set row

EMcK01

Board Regular
Joined
Jun 14, 2015
Messages
125
Hi,

I think this is straightforward, how do I stop the following code when it gets to row 5

VBA Code:
Sub InsertBlankRows2()

    Dim LR As Long, i As Long
    LR = Range("J" & Rows.Count).End(xlUp).Row
    For i = LR To 2 Step -1
                If Range("J" & i).Value <> Range("J" & i - 1).Value Then Rows(i).Resize(3).EntireRow.Insert
    Next i
End Sub

Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Change this:
Rich (BB code):
    For i = LR To 2 Step -1
to this:
Rich (BB code):
    For i = LR To 5 Step -1
Note what it is doing. It is going through the rows backwards, starting with the last row and working its way up.
So that number if the row number to stop at.
 
Upvote 0
Solution
Perfect, thank you. I thought it was something straight forward but couldn't get it earlier.
 
Upvote 0
You are welcome.
I hope that it makes sense, based on my explanation.

When inserting or deleting rows, it is usually best to loop through the rows backwards to avoid duplicating or missing rows (as you are changing the number of rows while you are trying to loop through them).
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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