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

nick612hayden

New Member
Joined
May 15, 2012
Messages
33
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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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