Loop and insert row VBA

Excelnoobisme

Board Regular
Joined
Nov 19, 2010
Messages
128
Hi,

Assuming on column A i have a set of numbers in ascending order.

i.e.
1
1
2
3
3
3
4
4

how can i run a loop and insert 2 row in between all the different number?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try:
Code:
Sub InsertRow()
Application.ScreenUpdating = False
Dim i As Long
i = 1
Do While Not IsEmpty(Range("A" & i))
    If Range("A" & i + 1) <> Range("A" & i) Then
        Rows(i + 1).Insert
        Rows(i + 1).Insert
        i = i + 2
    End If
    i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi. Try

Code:
Sub InsRow()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value <> Range("A" & i - 1).Value Then Rows(i).Resize(2).Insert
Next i
End Sub
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG07Mar39
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Lst = Range("A" & rows.Count).End(xlUp).row
[COLOR="Navy"]For[/COLOR] n = Lst To 2 [COLOR="Navy"]Step[/COLOR] -1
    [COLOR="Navy"]With[/COLOR] Range("A" & n)
        [COLOR="Navy"]If[/COLOR] .value <> .Offset(-1).value [COLOR="Navy"]Then[/COLOR]
            .EntireRow.Resize(2).Insert
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Run into some problem as my 1st Row is header. if i run, it will insert rows inbetween the header and 1st number which i dont want to. How can i avoid that?
 
Upvote 0
With the code I suggested earlier, change to:
Rich (BB code):
Sub InsertRow()
Application.ScreenUpdating = False
Dim i As Long
i = 2
Do While Not IsEmpty(Range("A" & i))
    If Range("A" & i + 1) <> Range("A" & i) Then
        Rows(i + 1).Resize(2).Insert
        i = i + 2
    End If
    i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
Change the number 2 to whatever row your data starts on (i.e. not 1 if 1 is the header row)
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,270
Members
452,902
Latest member
Knuddeluff

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