使用ws.protect的工作表保护集 - 但不使用菜单(查看 - 取消保护工作表)取消保护 [英] Worksheet protection set using ws.protect - but doesnt unprotect using the Menu (Review - Unprotect sheet)
问题描述
Private Sub password_protectallsheets()
对于工作表中的每个ws
ws.protect Password =edc1
下一个
End Sub
,但是当我尝试使用密码通过Excel 2007菜单(查看 - >取消保护工作表)取消保护工作表时,它表示您提供的密码不正确。
非常感谢任何帮助。
密码参数需要用:=指定 - 不只是=。对于一般方法的参数,这是正确的:
ws.protect密码:=edc1
$要使编译器捕获这些类型的错误,请务必在模块的开头使用Option Explicit,就像上面已经完成的那样。当您这样做时,编译器会给您一个变量未定义错误,当您忘记:选项显式将节省您大量的时间与其他类型的变量声明错误。要为所有新模块启用,请在VBE中检查工具>选项>编辑>需要变量声明。
我无法弄清楚的部分是为什么在代码中没有:,根本没有分配密码。我希望这张表受到保护,但没有密码。
I have the following code to protect the sheets of a workbook in Excel 2007
Private Sub password_protectallsheets()
For Each ws In Worksheets
ws.protect Password = "edc1"
Next
End Sub
but when I try to unprotect the sheet using the password through the Excel 2007 Menu (review -> Unprotect sheet), it says the password you have supplied is not correct.
Any help is hugely appreciated.
解决方案 The password argument needs to be specified with ":=" - not just "=". This is true for arguments to methods in general:
ws.protect Password := "edc1"
To have the compiler catch these types of errors, always use Option Explicit at the beginning of modules, as JMax has done above. When you do so, the compiler will give you a "Variable not Defined" error when you forget the ":" Option Explicit will save you lots of time with other types of variable declaration errors. To enable it for all new modules check Tools>Options>Edit>Require Variable Declaration in the VBE.
The part I can't figure out is why, in the code without the ":" a password is assigned at all. I would have expected the sheet to be protected, but without a password.
这篇关于使用ws.protect的工作表保护集 - 但不使用菜单(查看 - 取消保护工作表)取消保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!