Looping Copy and Paste

NavyPlayboy

New Member
Joined
Sep 26, 2011
Messages
2
Hello,

I am just getting back into using excel macros. This is the very basic macro I am using right now:

Sub AddColumn()
'
' AddColumn Macro
' Macro recorded 9/13/2011 by
'

'
Range("A1").Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("A1").Select
Selection.End(xlToRight).Offset(0, 1).Select
ActiveSheet.Paste
ActiveCell.EntireColumn.AutoFit
Range("A1").Select
End Sub


This copies the last column of data in my spreadsheet and pastes that data in the following column. It only does it once but I am trying to get it to repeat x number of times without having to re-run the macro. I eventually want to get to the point where a prompt will ask how many columns you would like to add and then have to macro loop that many times. But for now I would settle for just finding out how to get the dang thing to loop. Any help would be appreciated, and I'm a beginner, please be gentle :)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Welcome to the board.

You don't need to select cells to work with them. Something like this:

Code:
Sub AddColumn()
Dim LC As Long
Dim LR As Long
Dim No As Integer
Dim i As Long
No = InputBox("How many times would you like to copy/paste the column?", , 1)
 
LC = Range("A1").End(xlToRight).Column
LR = Cells(1, LC).End(xlDown).Row
Range(Cells(1, LC), Cells(LR, LC)).Copy
For i = 1 To No
    Cells(1, LC).Offset(, i).PasteSpecial xlPasteAll
Next i
Application.CutCopyMode = False
End Sub
 
Upvote 0
Thank you for your help!! That worked PERFECTLY! I'm trying to understand how this macro works though and was wondering if you could help?

I've never used the dim function but I get it, setting the dimensions so that something can be easily referenced later. But I'm confused about the LR one.

LR = Cells(1, LC).End(xlDown).Row

Is that saying select 1 cell to the right of where LC is located, then selecting the last cell down in this column? Why is '.Row' at the end?
 
Upvote 0
Thank you for your help!! That worked PERFECTLY! I'm trying to understand how this macro works though and was wondering if you could help?

I've never used the dim function but I get it, setting the dimensions so that something can be easily referenced later. But I'm confused about the LR one.

LR = Cells(1, LC).End(xlDown).Row

Is that saying select 1 cell to the right of where LC is located, then selecting the last cell down in this column? Why is '.Row' at the end?

You're welcome, thanks for the feedback.

That line of code tells Excel to start from the cell where row 1 meets the last column (Cells(1,LC). It then does the equivalent of holding Ctrl and pressing the down cursor key (End(xlDown). We then read the Row of this cell so that we know on which row your data ends.
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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