问题
The Contents parameter of the Worksheet.Protect method is TRUE by default (Microsoft Docs). Debugging my code I found that the ProtectContents property falls to FALSE if Worksheet.Protect is called again on an already protected sheet under certain circumstances. I do not understand why; is this a desired behaviour?
Here's the code I use:
Sheets(1).Protect _
Userinterfaceonly:=True, _
DrawingObjects:=False, _
Password:=MyPassword
' calling twice => ProtectContents falls to FALSE !
If I leave out the DrawingObjects parameter, then ProtedctContents remains TRUE after calling the code twice:
Sheets(1).Protect _
Userinterfaceonly:=True, _
Password:=MyPassword
' calling twice => ProtectContents remains TRUE
=> If the Protect method is called on an already protected sheet giving any parameter, the behaviour differs from calling Protect on an unprotected sheet?
My solution is to add Contents:=True explicitly:
Sheets(1).Protect _
Contents:=True, _
Userinterfaceonly:=True, _
DrawingObjects:=False, _
Password:=MyPassword
' calling twice => ProtectContents remains TRUE as stated
So I already know how to get my code to work, but I'd like to clearly understand the reason why.
来源:https://stackoverflow.com/questions/57679094/worksheet-protection-contents-defaults-to-false-after-calling-worksheet-protect