Locking cell range on all worksheets, partially working.

OfficeUser

Well-known Member
Joined
Feb 4, 2010
Messages
544
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am using this bit of code to perform a few things:
Code:
Sub Initialize()
Dim WS As Worksheet, shp As Shape
    For Each WS In Worksheets
        For Each shp In WS.Shapes
            If shp.Name = "lblGreen" Then shp.Visible = False
            If shp.Name = "lblRed" Then shp.Visible = True
    WS.Unprotect "password"
    WS.Range("AW10:BA10").Value = "80"
    WS.Range("S10:W10").Value = "80"
    WS.Range("AW10:BA10").Locked = True
    WS.Range("S10:W10").Locked = True
        Next shp
    WS.Protect "password"
    Next WS
End Sub

I receive an error: 'Unable to set the locked property of the range class' at this line:
Code:
WS.Range("S10:W10").Locked = True

If I remove the affected line then my code runs fine from beginning to end. Any suggestions on what may be the issue? Thanks.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If you are protecting your worksheets during Worksheet_Change, they are most likely getting protected again during these 2 lines:

Code:
    WS.Range("AW10:BA10").Value = "80"
    WS.Range("S10:W10").Value = "80"

If so, you could do one of 2 things, unprotect again:

Code:
    WS.Unprotect "password"
    WS.Range("AW10:BA10").Value = "80"
    WS.Range("S10:W10").Value = "80"
[COLOR=red][B]     WS.Unprotect "password"[/B][/COLOR]
    WS.Range("AW10:BA10").Locked = True
    WS.Range("S10:W10").Locked = True

Or, you could disable events so that Worksheet_Change code doesn't execute until after this routine is done:

Code:
    [B][COLOR=red]Application.EnableEvents = False[/COLOR][/B]
    WS.Range("AW10:BA10").Value = "80"
    WS.Range("S10:W10").Value = "80"
    WS.Range("AW10:BA10").Locked = True
    WS.Range("S10:W10").Locked = True
    [B][COLOR=red]Application.EnableEvents = True[/COLOR][/B]
 
Upvote 0
Should the changes to the ranges not be outside of the Shape loop? You would be re-applying the sheet protection multiple times.

Perhaps the password on the spreadsheet is different to "password" and so it has not been unlocked and you cannot change the cell properties?

Code:
Sub Initialize()
Dim WS As Worksheet, shp As Shape
For Each WS In Worksheets
    For Each shp In WS.Shapes
        If shp.Name = "lblGreen" Then shp.Visible = False
        If shp.Name = "lblRed" Then shp.Visible = True
    Next shp
    
    WS.Unprotect "password"
    WS.Range("AW10:BA10").Value = "80"
    WS.Range("S10:W10").Value = "80"
    WS.Range("AW10:BA10").Locked = True
    WS.Range("S10:W10").Locked = True
    WS.Protect "password"
    
Next WS

End Sub
 
Upvote 0
The shapes and ranges need to be changed on each sheet in the workbook. I am curious as to why the code will lock one range but not the other, seems very odd to me.
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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