gpt4 book ai didi

Excel VBA - 检查工作表是否受密码保护

转载 作者:行者123 更新时间:2023-12-02 14:31:06 28 4
gpt4 key购买 nike

我们可以使用 ProtectContents 属性检查工作表是否受到保护。但如何检查它是否受密码保护呢?

if ws.ProtectContents then
''do something
end if

最佳答案

我不认为有直接的方法可以通过属性来做到这一点。不过,您也可以尝试使用空白密码取消对工作表的保护,并在失败时捕获错误:

Function isSheetProtectedWithPassword(ws As Worksheet) As Boolean
If ws.ProtectContents Then
On Error GoTo errorLabel
ws.Unprotect ""
ws.Protect
End If
errorLabel:
If Err.Number = 1004 Then isSheetProtectedWithPassword = True
End Function

你可以这样调用它:

isSheetProtectedWithPassword(Worksheets("Sheet1"))

它将返回TrueFalse

关于Excel VBA - 检查工作表是否受密码保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40874165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com