Insert X rows below row Y based on value in row Y

lwhyatt

New Member
Joined
May 26, 2010
Messages
33
How can I get Excel to insert x rows below a row based on the value of a cell in that row?

eg if in row 1 cell B1 contains '5' I want 5 rows inserted.

I have a number of rows with different values so I need different gaps inserted between rows.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Does this work for you?

Code:
Sub addrows()


Dim rownum As Long
Dim addrow As Long


rownum = 1


Do Until Cells(rownum, 2).Value = ""
addrow = Cells(rownum, 2).Value
Rows(rownum + 1 & ":" & addrow + rownum).Insert Shift:=xlDown
rownum = rownum + addrow + 1
Loop


End Sub
 
Upvote 0
Great thanks. It's adding one too many rows each time though as I want to re-use the first row. What do I change if the cells specifying the number of rows to add are in I4:I439 ?
 
Upvote 0
Try this

Code:
Sub Macro2()
    Dim i As Long, n As Long
    For i = Range("I" & Rows.Count).End(xlUp).Row To 4 Step -1
        n = Cells(i, "I").Value
        If n > 0 Then Rows(i + 1 & ":" & i + n).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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