Question posted 2010 · +6 upvotes
I’m trying to write a macro that copies the content of column 1 from sheet 1 to column 2 on sheet 2. This is how the module looks like but, when I run it, I get
Run time error 9, Subscript out of range.
Sub OneCell()
Sheets("Sheet1").Select
'select column 1 A1'
Range("A1:A3").Select
Selection.Copy
Range("B1:B3").Select
ActiveSheet.Paste
Sheets("Sheet2").Select
Application.CutCopyMode = False
End Sub
Accepted answer +16 upvotes
The following works fine for me in Excel 2007. It is simple, and performs a full copy (retains all formatting, etc.):
Sheets("Sheet1").Columns(1).Copy Destination:=Sheets("Sheet2").Columns(2)
“Columns” returns a Range object, and so this is utilizing the “Range.Copy” method. “Destination” is an option to this method – if not provided the default is to copy to the paste buffer. But when provided, it is an easy way to copy.
As when manually copying items in Excel, the size and geometry of the destination must support the range being copied.
Excel VBA objects referenced (5)
Application— Using events with the Application objectApplication— Working with Other ApplicationsRange— Refer to Cells by Using a Range ObjectRange— Delete Duplicate Entries in a RangeSelection— Working with the Selection Object
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
— top 20%.