1
我们可以使用ProtectContents属性检查工作表是否受到保护。但是如何检查它是否被密码保护?Excel VBA - 检查工作表是否受密码保护
if ws.ProtectContents then
''do something
end if
我们可以使用ProtectContents属性检查工作表是否受到保护。但是如何检查它是否被密码保护?Excel VBA - 检查工作表是否受密码保护
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"))
,它将返回True
或False
谢谢JNevill。 –