Code works but fails on second run

heideway

New Member
Joined
Jul 18, 2011
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a routine that copies a range from one file to another. Both sheets are protected and the below code works fine when I run it the first time but gives an error the second time. If I save the files and open them again then run the same code it works correctly. Any help appreciated.


wbSource.Activate
ActiveWorkbook.Worksheets("Inputs").Visible = xlSheetVisible
ActiveWorkbook.Worksheets("Inputs").Select
ActiveWorkbook.Sheets("Inputs").Range("D20:DS307").Select
Selection.copy
ActiveWorkbook.Sheets("Inputs").Range("D20").Select

wbTarget.Activate
ActiveWorkbook.Worksheets("Inputs").Visible = xlSheetVisible
ActiveWorkbook.Worksheets("Inputs").Select
ActiveSheet.Unprotect Password:="Added_password_here"
ActiveWorkbook.Sheets("Inputs").Range("D20").Select
ActiveSheet.Paste

Error that is returned is: Run-time error '1004' Paste method of worksheet class failed
It seems to me that the clipboard is lost (i.e. the 'dotted lines' indicating the selected range on the source sheet) is "lost" when the target sheet is unprotected the second time the code run for some reason.

Any suggestions will be greatly appreciated.

Pete
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
maybe try
VBA Code:
wbSource.Activate
Worksheets("Inputs").Visible = xlSheetVisible
Sheets("Inputs").Range("D20:DS307").copy
wbTarget.Activate
Worksheets("Inputs").Visible = xlSheetVisible
Worksheets("Inputs").Unprotect Password:="Added_password_here"
Sheets("Inputs").Range("D20").Paste
 
Upvote 0
Thanks Michael, but your suggestion does not stop the issue.

I've managed to sort out by unprotecting the target sheet before copying the source sheet, and then I don't get the error. I then lock the target sheet after pasting.

The unprotecting seems to clear the clipboard contents for some reason.

Thanks for your help.

Regards,
Peter
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top