简体   繁体   中英

Copy data from one sheet to another to keep a log using vba

I'm trying to copy a table with data (from E5 to F18) to another sheet. The goal is to keep a log of the data so the it should always copy to the right of the last filled column.

So far I have the below but it keeps giving me the

Run-time error “1004”: apliccation-defined or object-defined error

Sub CopyPaste()
    With Sheets("Input")
        .Range("E5", .Range("E5").End(xlDown).End(xlToRight)).Copy
    End With

    With Sheets("Total")
        .Range("B2", Range("B2").End(xlToRight).Offset(0, 2)).PasteSpecial Paste:=xlValues
    End With
End Sub

In this part

With Sheets("Total")
    .Range("B2", Range("B2").End(xlToRight).Offset(0, 2)).PasteSpecial Paste:=xlValues
End With

The secocnd Range("B2")… is missing a leading dot. Therfore is the same as

With Sheets("Total")
    .Range("B2", ActiveSheet.Range("B2").End(xlToRight).Offset(0, 2)).PasteSpecial Paste:=xlValues
End With

And if the ActiveSheet is not the same sheet as is With Sheets("Total") this errors because you can't build a range out of different sheets.

Change it to

With Sheets("Total")
    .Range("B2", .Range("B2").End(xlToRight).Offset(0, 2)).PasteSpecial Paste:=xlValues
End With

to make sure both ranges are in the same sheet (and both statements use the With block)!

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