Copy/Paste any Selection N Times Vertically

alexluchini

New Member
Joined
Mar 14, 2014
Messages
4
Hello,

After a couple of hours of Googling and checking out these forums I decided to register and ask for help. I need a Macro that will Copy/Paste a selection of cells (that could change) "N" times vertically, without changing the order of the pasted selection.

I found a macro which ALMOST did what I wanted and I started hacking at it, this is close but still not correct, and it has errors:

Sub test()
Dim x As Integer
x = Application.InputBox("Number of Rows", "Number of Rows", Type:=1)
If x = False Then Exit Sub
Selection.Copy
Range(Selection.Offset(0), Selection.Offset(x)).EntireRow.Insert
Application.CutCopyMode = False
End Sub

Behavior:
I select a range, run the macro and an input box pops up asking how many times I'd like to paste the selected range. Perfect. The problem is, it doesn't offset and paste correctly.

Expected behavior:
When I enter the number of times I'd like to paste the range I'd like it to paste the range (without sorting, and keeping any formulas intact) vertically.

For example, if my Selected Range looked like this (keeping in mind my selected range could change):

Col ACol BCol C
Text 1Text 1Text 1
Text 2Text 2Text 2
Text 3Text 3Text 3

<colgroup><col><col><col></colgroup><tbody>
</tbody>

I would want my pasted data to look like this:
Col ACol BCol C
Text 1Other Text 1More Text 1
Text 2Other Text 2More Text 2
Text 3Other Text 3More Text 3
Text 1Other Text 1More Text 1
Text 2Other Text 2More Text 2
Text 3Other Text 3More Text 3
Text 1Other Text 1More Text 1
Text 2Other Text 2More Text 2
Text 3Other Text 3More Text 3
Text 1Other Text 1More Text 1
Text 2Other Text 2More Text 2
Text 3Other Text 3More Text 3

<colgroup><col><col><col></colgroup><tbody>
</tbody>


Any help I can get would be much appreciated! Thank you.
 

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.
Code:
Sub pgojt()
Dim mR As Range
On Error Resume Next
Set mR = Application.InputBox("Select your Range", , , , , , , 8)
    If mR Is Nothing Then MsgBox "Nothing Selected!", vbExclamation: Exit Sub
On Error GoTo 0
Dim HowManyTimes As Integer: HowManyTimes = InputBox("How Many Times", , 1)
    If HowManyTimes < 1 Then MsgBox "Value Entered is not valid!", vbExclamation: Exit Sub
mR.Copy mR.Offset(mR.Rows.Count).Resize(mR.Rows.Count * HowManyTimes)
End Sub
 
Upvote 0
Code:
Sub pgojt()
Dim mR As Range
On Error Resume Next
Set mR = Application.InputBox("Select your Range", , , , , , , 8)
    If mR Is Nothing Then MsgBox "Nothing Selected!", vbExclamation: Exit Sub
On Error GoTo 0
Dim HowManyTimes As Integer: HowManyTimes = InputBox("How Many Times", , 1)
    If HowManyTimes < 1 Then MsgBox "Value Entered is not valid!", vbExclamation: Exit Sub
mR.Copy mR.Offset(mR.Rows.Count).Resize(mR.Rows.Count * HowManyTimes)
End Sub


Dear VBAGEEK,

This code worked perfectly for me as well. Would you mind briefly explaining how the code actually works?

Many thanks!

Reagan
 
Upvote 0
Code:
Sub pgojt()
Dim mR As Range
On Error Resume Next
Set mR = Application.InputBox("Select your Range", , , , , , , 8)
    If mR Is Nothing Then MsgBox "Nothing Selected!", vbExclamation: Exit Sub
On Error GoTo 0
Dim HowManyTimes As Integer: HowManyTimes = InputBox("How Many Times", , 1)
    If HowManyTimes < 1 Then MsgBox "Value Entered is not valid!", vbExclamation: Exit Sub
mR.Copy mR.Offset(mR.Rows.Count).Resize(mR.Rows.Count * HowManyTimes)
End Sub

May i know where i need to change if i want to use that macro as Horizontally .
 
Upvote 0
Off the top of my head, you would need to change the last line to:

mR.Copy mR.Offset(, mR.Columns.Count).Resize(, mR.Columns.Count * HowManyTimes)
 
Upvote 0
VBA Greek

Thank you so much for your reply . :)

Before i ask, i changed the last line to :


mR.Copy mR.Offset(mR.Columns.Count).Resize(mR.Columns.Count * HowManyTimes)

I didn't know that i need to add "," before mR.Columns.Count and mR.Columns.Count . :)

Now it works.

I DO thank you for your reply . And sorry for my bad english . :)

 
Upvote 0
Code:
Sub pgojt()
Dim mR As Range
On Error Resume Next
Set mR = Application.InputBox("Select your Range", , , , , , , 8)
    If mR Is Nothing Then MsgBox "Nothing Selected!", vbExclamation: Exit Sub
On Error GoTo 0
Dim HowManyTimes As Integer: HowManyTimes = InputBox("How Many Times", , 1)
    If HowManyTimes < 1 Then MsgBox "Value Entered is not valid!", vbExclamation: Exit Sub
mR.Copy mR.Offset(mR.Rows.Count).Resize(mR.Rows.Count * HowManyTimes)
End Sub

Hi,
Instead of copy paste, is it possible to copy and insert copied rows?

Thanks
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

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