I am setting the Locked
property of a small range, but the code fails with the familiar 1004 cannot set the Locked property of the Range class
, similar to this problem.
- The range in question does not contain any merged cells
- My sheet is locked with
UserInterFaceOnly=True
I am out of ideas - how can I find out what is causing this to fail?
EDIT: I failed to mention that I am locking the sheet, saving it, closing and re-opening - and the code for setting the Locked
-property is triggered in Workbook_Open
.
This leads to a design issue caused by the fact that re-opening the workbook removes the interface-only protection, leaving the entire sheet protected. Thanks to @CLR for putting me on that path, and credit goes to him if he decides to submit it as an answer.
Lock code:
Sub LockSheet()
If ws1.ProtectContents = False Then ws1.Protect Password:="1", UserInterFaceOnly:=True
End Sub
Code snippet that fails when the sheet is protected (but works if I unprotect the sheet):
With summaryRange
.Locked = Not (someBoolVar) ' <-- 1004 Cannot set Locked etc.
.FormulaHidden = Not (someBoolVar)
End With
where summaryRange is qualified and working in other parts of the code:
Set summaryRange = ws1.Range(firstSummaryColumn & "4:" & lastSummaryColumn & lastRow)
& also verified working by the compiler:
? Module1.summaryRange.Address
$J$4:$M$50