VBA fails when worksheet is protected?

EssKayKay

Board Regular
Joined
Jan 5, 2003
Messages
233
Office Version
  1. 2007
Platform
  1. Windows
Hello,

I have a VBA routine that runs fine if the worksheet is unprotected. However, if it is protected it fails with a
Run-time error ‘1004’ -- Paste method of Worksheet class failed

It fails when attempting to run ActiveSheet.Paste. I placed a Sheets("Amortize").Unprotect line just prior to the ActiveSheet.Paste hoping this would unprotect the worksheet and allow the code to proceed – it still fails.

Here is one of the routines that is having the issue –

VBA Code:
Sub MaxData()
' Sets up Maximum DATA range to 2000 payments
' Keyboard Shortcut: Ctrl+e

Application.ScreenUpdating = False
Rows("34:2032").Select

     Sheets("Amortize").Unprotect                               '<=== This Unprotect works fine here ===

     Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
     Range("B2035:L2035").Select
     Selection.Copy 
     Range("B2032").Select
     Range(Selection, Selection.End(xlUp)).Select

     Sheets("Amortize").Unprotect                              '<=== This Unprotect does not work ===
     ActiveSheet.Paste                                                 '<=== This is where it fails ===
     Application.CutCopyMode = False
     Application.Run "Sheet1.cmdHome_Click"
End Sub


I also created a subroutine that unprotects the worksheet. I placed this in the above routine instead of the Sheets("Amortize").Unprotect but this too failed.

Code:
Sub UnProtect_It()
   Dim Ws As Worksheet
   For Each Ws In ActiveWorkbook.Worksheets
        Ws.Unprotect
    Next Ws
End Sub

Any suggestions would be appreciated.

Thanks for viewing,
Steve
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I'm still interested in fixing the problem above but I coded a workaround. It's not really the answer but rather a patch. It appears to be working.

Here's the patch
VBA Code:
Sheets("Amortize").Unprotect
    Range("B2035:K2035").Select
    Selection.Copy
    Range("B2032").Select
    Range(Selection, Selection.End(xlUp)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

Steve K.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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