简体   繁体   中英

VBA to paste value using Range.Copy method with Offset

I am currently using the following code to consolidate data from Multiple sheets with the same range using Range.Copy method using Offset.

I am trying to Paste only Values instead of formulas. But, I get the formulas also which is leading to Error "#REF!". Can anyone please help me with the correct syntax? I just started learning VBA coding.

    For Each ws In Sheets(Array("A", "B", "C", "D", "E"))
    ws.Activate
    bottomD = Range("BC" & Rows.Count).End(xlUp).Row
    Range("BC3:BE" & bottomD).Copy Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
    Next ws

You can do this without activating each sheet, and using pastespecial to copy values only

Sub x()

Dim ws As Worksheet, bottomD As Long

For Each ws In Sheets(Array("A", "B", "C", "D", "E"))
    bottomD = ws.Range("BC" & ws.Rows.Count).End(xlUp).Row
    ws.Range("BC3:BE" & bottomD).Copy
    Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlValues
Next ws

End Sub

You can directly assign values, not need to use copy/paste and no need to select/activate:

For Each ws In Sheets(Array("A", "B", "C", "D", "E"))
    With ws.Range("BE3", ws.Cells(ws.Rows.Count, "BC").End(xlUp))
        Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) _
           .Resize(.Rows.Count, .Columns.Count).value = .value
    End With
Next ws

This should get you what you need.

.PasteSpecial xlPasteValues

https://msdn.microsoft.com/en-us/library/office/ff839476.aspx?f=255&MSPPError=-2147217396

You need to use the .PasteSpecial method:

Range("BC3:BE" & bottomD).Copy
Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).pastespecial xlPasteValues

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