How can I copy columns from one sheet to another with VBA in Excel?

前端 未结 5 1787
抹茶落季
抹茶落季 2020-12-09 17:39

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

5条回答
  •  青春惊慌失措
    2020-12-09 18:16

    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.

提交回复
热议问题