Extract Worksheets Code Crashing

cterjesen

Board Regular
Joined
Jun 23, 2003
Messages
119
Any idea why this piece of code would generate an error message:

ActiveWindow.SelectedSheets.Copy

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
ActiveSheet.PageSetup.RightFooter = "Extracted from Budget " & Now & Chr(12)
Module1.UnProtectIt
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Cells.Select
Selection.Locked = True
Selection.FormulaHidden = False
Module1.ProtectIt

Next ws

....i get an error on the 4th worksheet it tries to export at the "Selection.Locked = True" line. the message says Locked Range not defined.

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
    Dim ws     As Worksheet

    ActiveWindow.SelectedSheets.Copy
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        ws.PageSetup.RightFooter = "Extracted from Budget " & Now & Chr(12)
        Module1.UnProtectIt
        With ws.UsedRange
            .Value = .Value
            .Locked = True
            .FormulaHidden = False
        End With
        Module1.ProtectIt
    Next ws
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
 
Upvote 0
For some reason it crashes when it reaches certain selected sheets. I get a Run-time error '-2147417848 (80010108) Method 'Locked' of object 'Range' failed. I have to Ctrl+Alt+Del out of Excel and restart when this happens.

Interestingly enough, if I manually try to extract the sheets that the code stops on, I can copy them to another workbook, but then I get a "calculation" messsage on the status bar that won't go away even by hitting F9 and every time I try to enter something in a cell, Excel tries to calculate slowly.
 
Upvote 0
Can you confirm that the code for UnProtectIt is actually unprotecting each worksheet (especially when called from this macro)?
 
Upvote 0
That's a good point. the code for that is:

ActiveSheet.Protect Password:="password", DrawingObjects:=True, Contents:=True, Scenarios:=True


What's funny is, this code works on an earlier version of this file from last year. Enhancements have been added this year, but I can't figure out what would have corrupted this piece.

And it still works for certain sheets, but not others. They all have the same password.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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