2014-11-02 82 views
0

嗨,我有以下代碼將數據從工作表1複製到工作表2中。所有列都已被選中,如何修改以僅選擇列A,I和M:從工作表1複製特定列到工作表2的vba代碼

Sub NewSheetData() 
    With Application 
     .ScreenUpdating = False 
     .EnableEvents = False 
    End With 

    Dim Rng As Range 

    Set Rng = Range([A1], Range("A" & Rows.Count).End(xlUp)) 

    On Error Resume Next 

    With Rng 
     .AutoFilter , field:=1, Criteria1:="*TC*", Operator:=xlOr 
     .SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Sheet2").Range("A1") 
     .AutoFilter 

    End With 

    On Error Goto 0 

    Application.EnableEvents = True 

回答

1

你可以做一個單獨的複製粘貼這樣的:

With Rng 
    .AutoFilter , field:=1, Criteria1:="*TC*", Operator:=xlOr 
    .SpecialCells(xlCellTypeVisible).Range("A1").Copy Sheets("Sheet2").Range("A1") 
    .SpecialCells(xlCellTypeVisible).Range("I1").Copy Sheets("Sheet2").Range("B1") 
    .SpecialCells(xlCellTypeVisible).Range("M1").Copy Sheets("Sheet2").Range("C1") 
    .AutoFilter 
End With 

編輯: 你也可以嘗試:

Sheets("Sheet1").Range("A1").SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet2").Range("A1") 
Sheets("Sheet1").Range("I1").SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet2").Range("A1") 
Sheets("Sheet1").Range("M1").SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet2").Range("A1") 
相關問題