VBA使用Range.Copy方法与Offset粘贴值

我目前正在使用下面的代码来使用Range.Copy方法使用Offset合并具有相同范围的多个工作表中的数据。

我正在尝试只粘贴值而不是公式。 但是,我也得到了导致错误“#REF!”的公式。 任何人都可以请帮助我正确的语法? 我刚开始学习VBA编码。

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 

您可以直接分配值,不需要使用复制/粘贴,也不需要select/激活:

 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 

您可以在不激活每个工作表的情况下执行此操作,并且只使用粘贴专门复制值

 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 

这应该得到你所需要的。

.PasteSpecial xlPasteValues

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

您需要使用.PasteSpecial方法:

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