Macro to insert blank rows below data based on value of specific cell

sneaky911

New Member
Joined
Feb 5, 2016
Messages
17
First of all I'd like to apologize. I've seen a few threads addressing my issue but I'm unfamiliar with code and cannot figure out how to modify it to fit my specific data. I have 5 columns of data. (A-E) I'd like to look at the value in column D and insert 1 less number of rows directly below the data and then copy the data for the entire row down. Example: If value in column D=4, I'd like to insert 3 blank rows and then copy down. If value in column D=1, no additional rows needed. Any help would be greatly appreciated.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Someone may be able to come up with something a little more slick, but here is something quick I whipped up:
Code:
Sub MyCopy()

    Dim lastRow As Long
    Dim myRow As Long
    Dim myColD As Variant
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column D
    lastRow = Cells(Rows.Count, "D").End(xlUp).Row
    
'   Loop through rows backwards, up to row 2
    For myRow = lastRow To 2 Step -1
'       Check value in column D and if greater than 1, insert rows and copy
        myColD = Cells(myRow, "D")
        If myColD > 1 Then
            Rows(myRow + 1 & ":" & myRow + myColD - 1).Insert
            Rows(myRow).Copy
            Rows(myRow + 1 & ":" & myRow + myColD - 1).Select
            ActiveSheet.Paste
            Application.CutCopyMode = False
        End If
    Next myRow

    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
I'm not suggesting anything wrong with Joe's code, but there are two possible tweaks that you might want to consider.

1. The insertion of the new rows to accept the copied data can be done as part of the copy/paste process rather than a separate operation.

2. There is no need to turn off the CutCopymode each time through the loop, it can be done just once at the end.

So the last part of the code could become ..
Rich (BB code):
'   Loop through rows backwards, up to row 2
    For myRow = lastRow To 2 Step -1
'       Check value in column D and if greater than 1, insert rows and copy
        myColD = Cells(myRow, "D")
        If myColD > 1 Then
            Rows(myRow).Copy
            Rows(myRow + 1).Resize(myColD - 1).Insert
        End If
    Next myRow
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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