2017-04-11 446 views
1

我正在使用以下代碼來使用偏移量使用Range.Copy方法合併來自具有相同範圍的多個工作表的數據。使用Range.Copy方法使用Offset對VBA進行粘貼值

我想只粘貼值而不是公式。但是,我也得到了導致錯誤「#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 

回答

1

您可以直接賦值,不需要使用複製/粘貼並且不需要選擇/激活:

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 
2

你可以做到這一點沒有激活每個片,並使用PasteSpecial的複製值僅

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 
+0

嗨SJR,它的工作。非常感謝你的幫助。 –

0

你需要使用.PasteSpecial方法:

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