使用vba将多个列复制并粘贴到另一个工作表中

时间:2017-03-15 16:02:09

标签: excel vba excel-vba

我正在编写一个代码,该代码将复制几个不按顺序排列的数据列。我无法弄清楚如何一步到位,所以我试图把它分成两部分。在第一组列帖子之后,我无法返回到我正在复制下一组列的表单。这就是我的代码到目前为止的样子。

Survey_Macro1宏

range("A:D").Select
range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add.Name = "Data"
ActiveSheet.Paste
ThisWorkbook.Save
ThisWorkbook.Sheets("Table").Activate
Application.CutCopyMode = False
range("AK:AL").Select
range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Worksheets("Data").range(E1).Select
ActiveSheet.Paste

1 个答案:

答案 0 :(得分:3)

请参阅How to avoid using Select in Excel VBA macros

Sub yg23iwyg()
    Dim wst As Worksheet

    Set wst = Worksheets.Add
    wst.Name = "Data"
    With Worksheets("Table")
        .Range(.Cells(1, "A"), .Cells(.Rows.Count, "D").End(xlUp)).Copy _
            Destination:=wst.Cells(1, "A")
        .Range(.Cells(1, "AK"), .Cells(.Rows.Count, "AL").End(xlUp)).Copy _
            Destination:=wst.Cells(1, "E")

        'alternate with Union'ed range - becomes a Copy, Paste Special, Values and Formats because of the union
        .Range("A:D, AK:AL").Copy _
            Destination:=wst.Cells(1, "A")
    End With

End Sub