簡體   English   中英

VBa復制錯誤“我們無法粘貼,因為復制和粘貼區域的大小不同”

[英]VBa Copy error “We cant paste because the copy and paste area are not the same size”

請幫助我調試此代碼。 我正在從100個文件中編寫報告。 該代碼適用於幾乎所有文件,但對於某些文件會彈出此錯誤。 所有文件的格式都相同,只是行數不同

sh.Range("A2:H" & lastRow).Copy 
Destination:=wb1.Worksheets("MainSheet").Range("A" & mainrow)

A2&last row = A2:H17408
"A" & mainrow=A1033559

出現此錯誤消息的原因有多種,具體情況如下:1.將合並的單元格粘貼到非合並的單元格時,反之亦然。 2.將過濾器應用於圖紙並嘗試粘貼與復制數據大小不同的紙張時。

您的代碼太有限,無法確保100%正確答案,因為我們看不到您如何定義變量。 但是,假設已正確定義它們,則最好使用以下命令:

Dim ws as worksheet
Set ws = wb1.Sheets("MainSheet")

' Range goes from columns A to H with mainrow and LastRow as variable rows
ws.Range(ws.Cells(mainrow, 1), ws.Cells(LastRow, 8)).Value = sh.Range(sh.Cells(mainrow, 1), sh.Cells(LastRow, 8)).Value

而不是復制粘貼。 除此之外,請確保正確定義了mainrow和LastRow。 如有疑問,請將您的代碼的這一部分添加到已編輯的問題中,以便我們進行研究。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM