copy rows in excel multiple times

cip888

New Member
Joined
Jul 18, 2014
Messages
11
Hi All

I have data in rows in one column(A), that I would like to paste onto another sheet n times in one column(X) separated by one row.

For example:

n=2

column A column X
A A
B A
C
B
B

C
C

Does anyone know how to setup a macro to achieve that?

Any help would be much appreciated, thanks in advance !
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this macro

Code:
Option Explicit

Sub CopyValuesMultipleTimes()
Dim rng As Range, rngCell As Range
Set rng = Selection

Dim x As Long
x = InputBox("How many times do you want to repeat each value?")

Dim wsDest As Worksheet
Set wsDest = Worksheets.Add(After:=ActiveSheet)
wsDest.Name = "Destination"

Dim rngDest As Range

Dim i As Long
For Each rngCell In rng
    If rngCell = rng(1) Then
        wsDest.Range(Cells(1, 1), Cells(x, 1)).Value = rngCell
    Else
        For i = 1 To x
            With wsDest
                If i = 1 Then
                    Set rngDest = .Cells(.Rows.Count, 1).End(xlUp).Offset(2, 0)
                Else
                    Set rngDest = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
                    End If
            End With
            rngDest = rngCell
        Next i
    End If
Next rngCell
End Sub

You will need to select the values you want to copy (my macro assumes that all the values are in one column).
The macro will insert a new worksheet called "Destination".
The macro will ask you how many times you would like to duplicate each value.
The macro will write each value multiple times to a new worksheet, inserting a rows as it goes.
I hope this helps.
 
Upvote 0
Try this macro

Code:
Option Explicit

Sub CopyValuesMultipleTimes()
Dim rng As Range, rngCell As Range
Set rng = Selection

Dim x As Long
x = InputBox("How many times do you want to repeat each value?")

Dim wsDest As Worksheet
Set wsDest = Worksheets.Add(After:=ActiveSheet)
wsDest.Name = "Destination"

Dim rngDest As Range

Dim i As Long
For Each rngCell In rng
    If rngCell = rng(1) Then
        wsDest.Range(Cells(1, 1), Cells(x, 1)).Value = rngCell
    Else
        For i = 1 To x
            With wsDest
                If i = 1 Then
                    Set rngDest = .Cells(.Rows.Count, 1).End(xlUp).Offset(2, 0)
                Else
                    Set rngDest = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
                    End If
            End With
            rngDest = rngCell
        Next i
    End If
Next rngCell
End Sub

You will need to select the values you want to copy (my macro assumes that all the values are in one column).
The macro will insert a new worksheet called "Destination".
The macro will ask you how many times you would like to duplicate each value.
The macro will write each value multiple times to a new worksheet, inserting a rows as it goes.
I hope this helps.

Hi Harry thank you for your prompt reply
.
I have tried your macro but after I am asked "how many times do you want to repeat each value?" i get a Run-time error '1004': Application-defined or object-defined error.

Also I noted that my questions was not very clear, I repost the last part here below:

For example:

n=2

column A
A
B
C

column X should be
A
A

B
B

C
C
 
Upvote 0
Hi Harry thank you for your prompt reply
.
I have tried your macro but after I am asked "how many times do you want to repeat each value?" i get a Run-time error '1004': Application-defined or object-defined error.

Also I noted that my questions was not very clear, I repost the last part here below:

For example:

n=2

column A
A
B
C

column X should be
A
A

B
B

C
C

That is exactly what the macro does. I am not sure why you are getting that error. I have tested this on both Excel 2010 and Excel 2013 and it runs fine.
See the following video for an illustration of how it runs (you can watch the video in your browser).

CopyValuesNTimes - aflashman's library

Please note that it assumes your values are in one column. Select the range of values (not the entire column). Make sure that what you are doing is similar to the way I run the macro in the video.

If you are entering the number requested by the input box via the number pad make sure you have NUM LOCK turned on otherwise you won't be entering a number.
 
Last edited:
Upvote 0
Thanks Harry

I followed your video and now the macro is working beautifully !! I think it might had something to do with the fact that I saved the macro on one sheet rather than in the ThisWorkbook object
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

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