Macro for inserting a certain # of rows

Kevin_

New Member
Joined
Aug 2, 2011
Messages
7
Hi - any help writing this macro is greatly appreciated. I have about 1000 rows of data (Row 1 is titles).

In Column L, I've used a countif on another tab to figure out the # of rows I want to insert below each row from the orginal data.

So, cell L2 may have the value 15, cell L3 has 9, cell L4 has 1, and cell L5 has zero.

I want the macro to insert 15 rows below row 2, then insert 9 rows below (what used to be) row 3, then 1 row below (what used to be) row 4, and not insert any rows below row 5.

I'd like this to work all the way to the end of my data.

I think this is pretty simple VBA but I'm a beginner. Thanks very much.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
So just to be clear, you have numbers in some cells in column L that represent how many rows you want to insert below those number-containing cells.

First, is that correct, and second for efficiency of execution purposes, are those numbers in column L being returned by formulas in those cells (you mentioned COUNTIF) or are those numbers in column L there because they were manually entered, meaning column L has no formulas.
 
Upvote 0
Yes and yes. For now, I want to insert 15 blank rows below row2. The value in cell L2 is a formula -a countif based off another tab that, in this case, yielded a result of 15. Thank you
 
Upvote 0
Try this on a copy of your worksheet:

Code:
Sub InsertRowz()
If WorksheetFunction.CountA(Columns(12)) > 1 Then
Application.ScreenUpdating = False
Dim cell As Range
For Each cell In Columns(12).SpecialCells(3)
Rows(cell.Row + 1).Resize(cell.Value).Insert
Next cell
Application.ScreenUpdating = True
End If
End Sub

The first line has a > 1 element assuming cell L1 contains a header label. If that is not the case, change > 1 to > 0.
 
Upvote 0
Hmmm.it worked without errors when I tested it. Is the sheet protected or are there any merged cells
 
Upvote 0
No, but when I hit debug, it seems to not like something to do with the first line (with Then at the end). Could be something I am doing wrong as I'm a little VBA challenged.

I did copy and paste but also - when you have Columns(l2) - it looks like the number 12. I assume it is L2, though.
 
Upvote 0
It is 12 as in twelve as in the twelfth column which is column L. Just copy and paste my macro as is, into a standard module, and it should work for you as it did for me.
 
Upvote 0
Inserting, like deleting, works best from the bottom up

Code:
Dim i As Long

With Sheet1.Range("L:L")
    For i = .Cells(.Rows.Count, 1).End(xlUp).Row to 2 Step - 1
        .Cells(i +1, 0).Resize(.Cells(i, 1), 1).EntireRow.Insert
    Next i
End With
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,462
Members
452,915
Latest member
hannnahheileen

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