2017-03-15 218 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

相關問題