ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Please explain copy/pastespecial issue (https://www.excelbanter.com/excel-programming/450850-please-explain-copy-pastespecial-issue.html)

[email protected]

Please explain copy/pastespecial issue
 
Can anyone explain the following problem:

If I run the following code

Sub Demo()

Dim shtSource As Worksheet
Dim shtTarget As Worksheet

Set shtTarget = Sheets("input")
Set shtSource = Sheets("output")

shtSource.Range("A1").CurrentRegion.Copy

With shtTarget
.Cells.Clear
.Range("A1").PasteSpecial
End With

End Sub

I get "Run time error 1004:

PasteSpecial method of Range class failed"

If I run the code as follows, I get no error and everything is fine:

Sub Demo()

Dim shtSource As Worksheet
Dim shtTarget As Worksheet

Set shtTarget = Sheets("input")
Set shtSource = Sheets("output")

shtTarget.Cells.Clear
shtSource.Range("A1").CurrentRegion.Copy

With shtTarget
.Range("A1").PasteSpecial
End With

End Sub

To my way of thinking it makes no sense to be bouncing between the source and target objects. However, what I think is logical is bombing. Can one of the experts enlighten me?

Art

isabelle

Please explain copy/pastespecial issue
 
hi,

you should not delete the cell contents between action of copy and paste

Sub Demo()
Dim shtSource As Worksheet
Dim shtTarget As Worksheet
Set shtTarget = Sheets("input")
Set shtSource = Sheets("output")

With shtTarget
.Cells.Clear
shtSource.Range("A1").CurrentRegion.Copy
.Range("A1").PasteSpecial
End With
End Sub

isabelle


nuochoa080415

xem truyện tranh mirano nhà tôi Một nhóm học sinh đi chung trên một chiếc xe máy, đang chạy xe trên đường thì bị cảnh sát thổi, cả nhóm dừng lại.

Cảnh sát hỏi:"Tại sao mấy đứa dám tống ba mà lại không đội nón bảo hiểm vậy hả? Có biết như vậy là vi phạm luật giao thông hay ko?"

Cả nhóm hốt hoảng quay lại nhìn phía sau và hét lên:"Chết cha còn thằng thứ tư đu mất tiêu rồi!!!"

Cảnh sát: !!!???Bật ngửa Xem thêm xem truyện tranh nữ hoàng rắc rối


All times are GMT +1. The time now is 08:57 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com