2017-03-15 177 views
2

我正在研究一个代码,该代码将复制多个不按顺序的数据列。我无法弄清楚如何一步完成,所以我试图把它弄成两个。在第一组专栏文章后,我无法将其重新回到我正在复制下一组专栏的工作表。这就是我的代码到目前为止的样子。使用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 

回答

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 
+0

非常感谢你Jeeped,这是一个巨大的帮助! – Cocoberry2526

相关问题