使用ws.protect的工作表保护集 - 但不使用菜单(查看 - 取消保护工作表)取消保护 [英] Worksheet protection set using ws.protect - but doesnt unprotect using the Menu (Review - Unprotect sheet)

查看:340
本文介绍了使用ws.protect的工作表保护集 - 但不使用菜单(查看 - 取消保护工作表)取消保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Private Sub password_protectallsheets()
对于工作表中的每个ws
ws.protect Password =edc1
下一个
End Sub

,但是当我尝试使用密码通过Excel 2007菜单(查看 - >取消保护工作表)取消保护工作表时,它表示您提供的密码不正确。



非常感谢任何帮助。

解决方案

密码参数需要用:=指定 - 不只是=。对于一般方法的参数,这是正确的:

  ws.protect密码:=edc1
选项>编辑>需要变量声明。



我无法弄清楚的部分是为什么在代码中没有:,根本没有分配密码。我希望这张表受到保护,但没有密码。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆