Worksheet.Protection Contents defaults to FALSE after calling Worksheet.Protect twice

会有一股神秘感。 提交于 2021-02-10 11:50:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!