![](/img/trans.png)
[英]VBA You can't paste this here because the Copy and paste area aren't the same size - error
[英]We can't paste Excel ranges because the copy area and paste area aren't the same size
我想遍歷Worksheet1中的A列,並找到第一個具有指定文本“ Oil Production”的單元格。 該單元格是我要復制到Worksheet2的數組中的第一個單元格。 該單元格和數組的大小會不時更改,因此我使用了代碼。 然后,將其粘貼到Worksheet2中的B7單元格中,該單元格將永遠不會更改。
這是我的公式。 我在ActiveSheet.Paste
行出現錯誤
Sub Test()
Application.ScreenUpdating = False
For Each Cell In Sheets("Sheet1").Range("A:A")
If Cell.Value = "Oil Production" Then
ActiveSheet.Cells.Select
Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).Column)).Select
Selection.Copy
Sheets("Sheet2").Select
Range("B7").Select
ActiveSheet.Paste
End If
Next
End Sub
調整區域大小:
Sub Test()
Dim MyRowCount As Long, MyColCount As Long
Application.ScreenUpdating = False
For Each Cell In Sheets("Sheet1").Range("A1:A" & Range("A" & Rows.count).end(xlup).row) 'This make it poll the used data rather than the whole column
If Cell.Value = "Oil Production" Then
ActiveSheet.Cells.Select
With Range(ActiveCell, Cells(ActiveCell.End(xlDown).Row, ActiveCell.End(xlToRight).column))
.Copy
MyRowCount = .Rows.Count
MyColCount = .Columns.Count
End With
Sheets("Sheet2").Select
Range("B7").Resize(MyRowCount, MyColCount).PasteSpecial xlPasteAll
'Do you need to flick back to Sheet1 after pasting?
End If
Next
End Sub
我也為您挑選了很多選擇。
Range("A1").Select
Selection.Paste
可以寫成
Range("A1").PasteSpecial XLPasteAll
您可以用這種方法切出大多數選擇,您可以看到我也正在復制您的范圍
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.