简体   繁体   中英

Copy and paste fix number of rows from all the sheets and paste into one sheet in VBA

I want to Copy data of ("A1:A10") & ("D1:D10") columns from 10 sheets and paste it into a new sheet called("New1"). while it is copying the data it should not consider the new sheet("New1") as it is the results file. and all the results should be appending under each other

Below is the code that i've tried . But I am getting an error "function not defined" on "Next ws"

Dim ws As Worksheet 

Application.DisplayAlerts = False 

For Each ws In ActiveWorkbook.Worksheets 

    Range("b3").Select 
    Range(Selection, Selection.End(xlDown)).Select 
    Selection.Copy 
    Sheets("New1").Select
    Range("A1").Select 
    ActiveSheet.Paste 

Next ws 

Application.DisplayAlerts = True 

End Sub 

you could use this:

Dim ws As Worksheet

Application.DisplayAlerts = False

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "New1" Then ' if current sheet isn't "New1" one
        With ws ' reference current sheet
            .Range("b3", .Cells(.Rows.Count, "B").End(xlUp)).Copy Destination:=Sheets("New1").Cells(Rows.Count, "A").End(xlUp).Offset(1) ' copy referenced sheet range from B3 down to last not empty cell and paste it form first empty cell of sheet "New1" column A
        End With
    End If
Next ws

Application.DisplayAlerts = True

and easily tweak it to handle different copying and pasting ranges

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