简体   繁体   中英

vba code to copy specific columns from worksheet1 to worksheet2

Hi I have the following code to copy data from sheet1 to sheet 2. All columns are getting selected, How can I modify to pick column A, I, and M only:

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 

You can do an individual Copy Paste like this:

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

Edit: you can also try:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM