Copy Row x number of times from specific row and paste,shift down (VBA)

JJCam

New Member
Joined
Dec 28, 2018
Messages
6
Hi,

I need to copy a row and insert x number of times below (thus shifting down the below rows).
The x is a value from another cell.
1724570279988.png

I am struggling with my code as all I managed to succeed is copying the row and pasting x number of times but it overwrites the below row, it doesn't shift down and I can't find the solution.

Here is my code:

Sub copy_paste_range()
Dim r As Range, i As Long, n As Long
Set r = Range("7:7")
If Not IsNumeric(Range("A2").Value) Then Exit Sub
For i = 1 To Range("A2").Value
n = n + r.Rows.Count
r.Copy r.Offset(n)
Next
End Sub

Thanks for your help!

JJCam
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Maybe something like
VBA Code:
Sub copy_paste_range()
    Dim x As Integer, rng As Range

    If Not IsNumeric(Cells(2, "A").Value) Then Exit Sub
    x = Cells(2, "A").Value

    Set rng = Range("A7:D7")

    rng.Offset(1).Resize(x).Insert xlDown
    rng.Copy rng.Offset(1).Resize(x)

End Sub

Before
Book1
ABCD
1
25
3
4
5
6
7rowtocopy from
8
9
10Notesandcomments row
Sheet2


After

Book1
ABCD
1
25
3
4
5
6
7rowtocopy from
8rowtocopy from
9rowtocopy from
10rowtocopy from
11rowtocopy from
12rowtocopy from
13
14
15Notesandcomments row
Sheet2
 
Last edited:
Upvote 0
Solution
Hi,
Thanks so much for the quick answer. It works like a charm!!
I will check the documentation to understand better the resize and insert properties.

Thanks again, I really appreciate it. I was really desperate after hours trying to make it work.
 

Attachments

  • 1724574465033.png
    1724574465033.png
    17.6 KB · Views: 4
  • 1724574633287.png
    1724574633287.png
    5.7 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,105
Members
453,021
Latest member
Justyna P

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