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

lwhyatt

New Member
Joined
May 26, 2010
Messages
29
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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

mrshl9898

Well-known Member
Joined
Feb 6, 2012
Messages
1,951
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
 

lwhyatt

New Member
Joined
May 26, 2010
Messages
29
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 ?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,778
Office Version
  1. 2010
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,713
Messages
5,833,269
Members
430,200
Latest member
ADLHMA2022

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
Top