Copy/Paste a Column X number of Times

NocoffeeNoWorkee

New Member
Joined
Apr 28, 2016
Messages
12
The question seems simple but I cant find an answer.

Using a macro, I need to copy and paste a column X number of times to the right on the same sheet. X is given by cell A1 on Sheet 2. The rows in the column will probably vary each time I import data.

I'm using excel 2013. I cant write this with the macro recorder and I'm a noob.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Your quote:
I need to copy and paste a column X number of times
What column on sheet(2) do we want to copy x number of times?
 
Upvote 0
I wanted to copy Sheet 1 column A and get X from a certain cell. I thought sheet 2 A1 would be convenient for X because the amount of rows will change.
 
Upvote 0
Try this:
It gets the number of times from a Inputbox

Code:
Sub Copy_Columns()
Application.ScreenUpdating = False
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim i As Long
Dim num As Long
On Error GoTo M

num = InputBox("How many times do you want to copy Column ""A""?")
    For i = 2 To num + 1
        Range("A1" & ":" & "A" & Lastrow).Copy Destination:=Cells(1, i)
    Next
    Application.ScreenUpdating = True

    Exit Sub
    
M:
MsgBox "That is a invalid number"
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Try this:
It gets the number of times from a Inputbox

Code:
Sub Copy_Columns()
Application.ScreenUpdating = False
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim i As Long
Dim num As Long
On Error GoTo M

num = InputBox("How many times do you want to copy Column ""A""?")
    For i = 2 To num + 1
        Range("A1" & ":" & "A" & Lastrow).Copy Destination:=Cells(1, i)
    Next
    Application.ScreenUpdating = True

    Exit Sub
    
M:
MsgBox "That is a invalid number"
Application.ScreenUpdating = True

End Sub
You can simplify your code a little bit...
Code:
[table="width: 500"]
[tr]
	[td]Sub Copy_Columns()
  Dim Num As Long
  Application.ScreenUpdating = False
  Num = Application.InputBox("How many times do you want to copy Column ""A""?", Type:=1)
  If Not Num Like "*[!0-9]*" Then
    Columns("A").Copy Columns("B").Resize(, Num)
  Else
    MsgBox "Whole numbers greater than zero only!"
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,554
Members
449,237
Latest member
Chase S

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