[英]Copy values from different sheets and paste it in a master file
我想創建一些VBA代碼以從多個工作表中復制值並將其粘貼到一個主文件中。
我有以下步驟:
轉到工作表並選擇范圍
Sheets("V01 DEN HAAG").Select Range("H7").Select Range(Selection, Selection.End(xlToRight)).Select Selection.Copy
轉到主文件/選擇最低行,將其偏移一並粘貼值
Sheets("DATASET").Select Range("B3").Select Range(Selection, Selection.End(xlDown)).Select Selection.End(xlDown).Select ActiveCell.Offset(1, 0).Select ActiveCell.Paste
盡管最后一個“ ActiveCell.Paste”語句似乎有一個小錯誤。 它給了我錯誤:
對象不支持此屬性或方法。
有什么想法出了什么問題嗎?
ActiveCell
是Excel.Range
類的對象,並且此類沒有Paste
這樣的方法。 您需要改為使用PasteSpecial
並將參數Paste
設置為xlPasteAll
:
ActiveCell.PasteSpecial xlPasteAll
但是,在復制/粘貼范圍時選擇范圍不是一個好習慣。 相反,您應該使用Range
類型的變量。 下面是一個示例如何使用變量執行相同任務的示例:
Sub x()
Dim sourceRange As Excel.Range
Dim destinationRange As Excel.Range
'----------------------------------------------------------------------------------
'Set the reference to [sourceRange].
Set sourceRange = Sheets("V01 DEN HAAG").Range("H7", Range("H7").End(xlToRight))
'Find the initial cell.
Set destinationRange = Sheets("DATASET").Range("B3").End(xlDown).Offset(1, 0)
'Resize [destinationRange] to the same size as [sourceRange].
With sourceRange
Set destinationRange = destinationRange.Resize(.Rows.Count, .Columns.Count)
End With
'Actual copying & pasting.
Call sourceRange.Copy
Call destinationRange.PasteSpecial(xlPasteAll)
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.