# Macro: If cell contains value, then insert rows, based on cell value in a column

#### nick612hayden

##### New Member
Hey All

I'm am trying to figure out a macro to run that:

If cell on Column B (let's say B5) equals "1" "2" or "3"

Then insert (below) the the number of rows that the cell in F5 equals

So, if "1" shows up in cell B5, and if F5 equals 3, then 3 blank rows would be added below row 5

Then, go to the next row with data (after the rows that just were inserted) and repeat until there is no more data in the Workbook.

So now, if "2" shows up in the next cell (now, B9) and if F9 equals 2, then 2 blank rows would be added below row 9.

If the number doesn't show up in the cell in Column B, go to the next row and perform the macro.

Any help would be awesome, thanks!!! :D

#### JoeMo

##### MrExcel MVP
Try this:
Code:
``````Sub InsertRowsIf()
Dim lr As Long, R As Range
lr = Range("B" & Rows.Count).End(xlUp).Row
Set R = Range("B1", "B" & lr)
Application.ScreenUpdating = False
For i = R.Rows.Count - 1 To 1 Step -1
If R.Cells(i, 1).Value Like "[1-3]" Then
If IsNumeric(R.Cells(i, 1).Offset(0, 4).Value) Then
If R.Cells(i, 1).Offset(0, 4).Value > 0 Then
R.Cells(i, 1).Offset(1, 0).Resize(R.Cells(i, 1).Offset(0, 4).Value, 1).EntireRow.Insert
End If
End If
End If
Next i
End Sub``````

• thefirstslice

#### nick612hayden

##### New Member
Worked like a charm JoeMo! Thanks!!!

Solved.

Last edited:

#### JoeMo

##### MrExcel MVP
Worked like a charm JoeMo! Thanks!!!

#### lemarve

##### New Member
Hi JoeMo,

Nice code, it works like a charm.
I wanted some help modifiying it.
I have column B as:

B1 2
B2 1
B3 3
B4 2

I want to insert number of of rows with the values in the column so you would have. I dont want a restriction on the number of rows, so long as the column is not empty.

B1 2
B2
B3
B4 1
B5
B6 3
B7
B8
B9
B10 2
B11
B12

#### JoeMo

##### MrExcel MVP
Hi JoeMo,

Nice code, it works like a charm.
I wanted some help modifiying it.
I have column B as:

B1 2
B2 1
B3 3
B4 2

I want to insert number of of rows with the values in the column so you would have. I dont want a restriction on the number of rows, so long as the column is not empty.

B1 2
B2
B3
B4 1
B5
B6 3
B7
B8
B9
B10 2
B11
B12
Welcome to the board!

Try this:
Code:
``````Sub InsertRowsIf()
Dim lr As Long, R As Range, i As Long
lr = Range("B" & Rows.Count).End(xlUp).Row
Set R = Range("B1", "B" & lr)
Application.ScreenUpdating = False
For i = R.Rows.Count To 1 Step -1
If IsNumeric(R.Cells(i, 1).Value) And Not IsEmpty(R.Cells(i, 1)) Then
R.Cells(i, 1).Offset(1, 0).Resize(R.Cells(i, 1).Value).EntireRow.Insert
End If
Next i
Application.ScreenUpdating = True
End Sub``````

#### lemarve

##### New Member
Hi,
Thank you Joe. Many thanks.
Worked exactly!!!!!!!! #### JoeMo

##### MrExcel MVP
Hi,
Thank you Joe. Many thanks.
Worked exactly!!!!!!!! You are welcome - thanks for the reply.

#### thefirstslice

##### New Member
Try this:
Code:
``````Sub InsertRowsIf()
Dim lr As Long, R As Range
lr = Range("B" & Rows.Count).End(xlUp).Row
Set R = Range("B1", "B" & lr)
Application.ScreenUpdating = False
For i = R.Rows.Count - 1 To 1 Step -1
If R.Cells(i, 1).Value Like "[1-3]" Then
If IsNumeric(R.Cells(i, 1).Offset(0, 4).Value) Then
If R.Cells(i, 1).Offset(0, 4).Value > 0 Then
R.Cells(i, 1).Offset(1, 0).Resize(R.Cells(i, 1).Offset(0, 4).Value, 1).EntireRow.Insert
End If
End If
End If
Next i
End Sub``````
JoeMo, you rock!!

I just want to take it a step above, I have information in columns A-J, is there a way that the entire row's information can be copied onto the new rows we created using this macro?
So for example, if I have row 2 creating 3 more rows below it, all the same exact information is carried over?

1,082,106
Messages
5,363,180
Members
400,720
Latest member
Pettel

### This Week's Hot Topics

• populate from drop list with multiple tables
Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
• Find list of words from sheet2 in sheet1 before a comma and extract text vba
Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
• Dynamic Formula entry - VBA code sought
Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...