0
我正在寫一個循環遍歷「源」工作表的宏,並且爲列A中的每個值複製範圍從模板工作表到目標工作表。在複製模板範圍之後,我需要根據源工作表值更改目標工作表中的幾個值。現在我試圖讓副本工作。複製失敗,錯誤1004'由於「複製」區域和粘貼區域的大小不一樣,因此無法粘貼信息。將範圍複製到另一個工作表的底部
Sub CopyRangeFromOneSheetToAnother()
Dim iLastRow As Long
Dim wb As Workbook
Dim shtSource As Worksheet
Dim shtTemplate As Worksheet
Dim shtDest As Worksheet
Dim sResourceName
Dim rngCalcTemplate As Range
Set wb = ThisWorkbook
Set shtSource = wb.Sheets(1)
Set shtTemplate = wb.Sheets("res_tpl")
Set shtDest = wb.Sheets.Add
'--set range for copying. Hard-coded for now would be nice if it would auto shrink/expand
Set rngCalcTemplate = shtTemplate.Range("A2:M7")
'Find the last row (in column A) with data.
iLastRow = shtSource.Range("A:A").Find("*", searchdirection:=xlPrevious).Row
'--loop through source sheet and copy template range to dest for each
For iSourceSheetRow = 2 To iLastRow
sResourceName = shtSource.Cells(iSourceSheetRow, 1)
rngCalcTemplate.Copy shtDest.Range("A" & Rows.Count).End(xlDown)
Next
End Sub
你需要'Rows.Count'之前添加紙張太,'shtDest。範圍(「A」&shtDest.Rows.Count).End(xlDown)'。否則,'rows.count'將在任何'ActiveSheet'上運行。不知道這是否會解決它,但它應該有所幫助。 (如果這不能解決問題,您只需將paste * to *範圍調整爲與複製的大小相同)。 – BruceWayne