Adding lines based on cell value

rubinda

New Member
Joined
Jun 26, 2018
Messages
36
I have a file that I am trying to copy specific cells down based on the number in column E.

If a cell in column E>1, I want to copy the following cells in a column down that many lines (for example: if the number in column E is 4, I will need 3 additional lines copied/pasted below, for a total of 4 lines). CELLS IN A COLUMN THAT NEED TO BE COPIED (A, B, F, G and H).

If a cell in column E=1, I want nothing to happen.

This is advanced for my experience in coding. Any help is appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this out

Code:
Sub ColumnE()
Dim r As Range
Dim rrow As Integer
'change the line below to the size of your data 
For Each r In Range("E1:E15")
    If r.Value > 1 Then
        rrow = r.Row
        Rows((rrow + 1) & ":" & (rrow + r.Value - 1)).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("A" & rrow & ":B" & rrow).Select
        Selection.AutoFill Destination:=Range("A" & rrow & ":B" & rrow + r.Value - 1), Type:=xlFillCopy
        Range("A" & rrow & ":B" & rrow).Select
        Range("F" & rrow & ":H" & rrow).Select
        Selection.AutoFill Destination:=Range("F" & rrow & ":H" & rrow + r.Value - 1), Type:=xlFillCopy
    End If
Next r
End Sub
 
Last edited:
Upvote 0
I get a run type error 13, type mismatch error that points to: Rows((rrow + 1) & ":" & (rrow + r.Value - 1)).Select
 
Upvote 0
that is so weird.... i just tried it again and it worked fine....

im honestly out of ideas on this .. hopefully someone else can chime in
 
Upvote 0
I had to change the range in column E and now it works. Thank you.

The only thing I need to change is that it deletes the data in column C. How do I prevent that from happening?
 
Upvote 0
Sorry for the late reply ... change the B to a C to include column C I highlighted it in red

Code:
Sub ColumnE()
Dim r As Range
Dim rrow As Integer
'change the line below to the size of your data 
For Each r In Range("E1:E15")
    If r.Value > 1 Then
        rrow = r.Row
        Rows((rrow + 1) & ":" & (rrow + r.Value - 1)).Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("A" & rrow & ":[COLOR=#ff0000]C[/COLOR]" & rrow).Select
        Selection.AutoFill Destination:=Range("A" & rrow & ":[COLOR=#ff0000]C[/COLOR]" & rrow + r.Value - 1), Type:=xlFillCopy
        Range("A" & rrow & ":[COLOR=#ff0000]C[/COLOR]" & rrow).Select
        Range("F" & rrow & ":H" & rrow).Select
        Selection.AutoFill Destination:=Range("F" & rrow & ":H" & rrow + r.Value - 1), Type:=xlFillCopy
    End If
Next r
End Sub
[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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