Worksheet.Protect 方法 (Excel)Worksheet.Protect method (Excel)

保护工作表使其不被修改。Protects a worksheet so that it cannot be modified.

语法Syntax

表达式保护PasswordDrawingObjects内容场景UserInterfaceOnlyAllowFormattingCellsAllowFormattingColumnsAllowFormattingRowsAllowInsertingColumnsAllowInsertingRowsAllowInsertingHyperlinksAllowDeletingColumnsAllowDeletingRowsAllowSortingAllowFilteringAllowUsingPivotTables)expression.Protect (Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)

_表达式_一个代表**工作表** 对象的变量。expression A variable that represents a Worksheet object.

ParametersParameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
PasswordPassword 可选Optional VariantVariant 指定工作表或工作簿的区分大小写密码的字符串。A string that specifies a case-sensitive password for the worksheet or workbook. 如果省略此参数,可以不使用密码地解除对工作表或工作簿的保护。If this argument is omitted, you can unprotect the worksheet or workbook without using a password. 否则,必须指定密码才能解除对工作表或工作簿的保护。Otherwise, you must specify the password to unprotect the worksheet or workbook. 如果忘记密码,则无法解除对工作表或工作簿的保护。If you forget the password, you cannot unprotect the worksheet or workbook.

请使用组合大小写字母、数字和符号的强密码。Use strong passwords that combine uppercase and lowercase letters, numbers, and symbols. 弱密码不混合使用这些元素。Weak passwords don't mix these elements. 例如,强密码:Y6dh!et5。Strong password: Y6dh!et5. 弱密码:House27。Weak password: House27. 密码的长度应为 8 个或更多字符。Passwords should be 8 or more characters in length. 使用 14 个或更多字符的通行短语更好。A pass phrase that uses 14 or more characters is better.

请务必记住您的密码。It is critical that you remember your password. 如果您忘记了密码,Microsoft 无法找回它。If you forget your password, Microsoft cannot retrieve it. 请将您编写的密码保存在安全的位置并远离它们要帮助保护的信息。Store the passwords that you write down in a secure place away from the information that they help protect.
DrawingObjectsDrawingObjects 可选Optional VariantVariant 如果为 True,则保护形状。True to protect shapes. 默认值为 TrueThe default value is True.
ContentsContents 可选Optional VariantVariant 如果为 True,则保护内容。True to protect contents. 对于图表,这将保护整个图表。For a chart, this protects the entire chart. 对于工作表,这将保护锁定单元格。For a worksheet, this protects the locked cells. 默认值为 TrueThe default value is True.
ScenariosScenarios 可选Optional VariantVariant 如果为 True,则保护方案。True to protect scenarios. 此参数仅对工作表有效。This argument is valid only for worksheets. 默认值为 TrueThe default value is True.
UserInterfaceOnlyUserInterfaceOnly 可选Optional VariantVariant 如果为 True,则保护用户界面,但不保护宏。True to protect the user interface, but not macros. 如果省略此参数,保护将应用于宏和用户界面。If this argument is omitted, protection applies both to macros and to the user interface.
AllowFormattingCellsAllowFormattingCells 可选Optional VariantVariant 如果为 True,则允许用户格式化受保护工作表上的任何单元格。True allows the user to format any cell on a protected worksheet. 默认值为 FalseThe default value is False.
AllowFormattingColumnsAllowFormattingColumns 可选Optional VariantVariant 如果为 True,则允许用户格式化受保护工作表上的任何列。True allows the user to format any column on a protected worksheet. 默认值为 FalseThe default value is False.
AllowFormattingRowsAllowFormattingRows 可选Optional VariantVariant 如果为 True ,则允许用户格式化受保护的工作表上的任何行。True allows the user to format any row on a protected worksheet. 默认值为 FalseThe default value is False.
AllowInsertingColumnsAllowInsertingColumns 可选Optional VariantVariant 如果为 True,则允许用户在受保护工作表上插入列。True allows the user to insert columns on the protected worksheet. 默认值为 FalseThe default value is False.
AllowInsertingRowsAllowInsertingRows 可选Optional VariantVariant 如果为 True,则允许用户在受保护工作表上插入行。True allows the user to insert rows on the protected worksheet. 默认值为 FalseThe default value is False.
AllowInsertingHyperlinksAllowInsertingHyperlinks 可选Optional VariantVariant 如果为 True ,则允许用户在受保护的工作表上插入超链接。True allows the user to insert hyperlinks on the protected worksheet. 默认值为 FalseThe default value is False.
AllowDeletingColumnsAllowDeletingColumns 可选Optional VariantVariant 如果为 True,则允许用户在受保护的工作表上删除列,其中要删除的列中的每个单元格都处于解锁状态。True allows the user to delete columns on the protected worksheet, where every cell in the column to be deleted is unlocked. 默认值为 FalseThe default value is False.
AllowDeletingRowsAllowDeletingRows 可选Optional VariantVariant 如果为 True,则允许用户在受保护的工作表上删除行,其中要删除的行中的每个单元格都处于解锁状态。True allows the user to delete rows on the protected worksheet, where every cell in the row to be deleted is unlocked. 默认值为 FalseThe default value is False.
AllowSortingAllowSorting 可选Optional VariantVariant 如果为 True,则允许用户对受保护工作表进行排序。True allows the user to sort on the protected worksheet. 排序范围内的每个单元都必须处于解锁或不受保护的状态。Every cell in the sort range must be unlocked or unprotected. 默认值为 FalseThe default value is False.
AllowFilteringAllowFiltering 可选Optional VariantVariant 如果为 True,则允许用户在受保护的工作表上设置筛选。True allows the user to set filters on the protected worksheet. 用户可以更改筛选条件,但无法启用或禁用自动筛选。Users can change filter criteria but can not enable or disable an auto filter. 用户可以在现有自动筛选上设置筛选。Users can set filters on an existing auto filter. 默认值为 FalseThe default value is False.
AllowUsingPivotTablesAllowUsingPivotTables 可选Optional VariantVariant 如果为 True ,则允许用户在受保护的工作表上使用数据透视表。True allows the user to use PivotTable reports on the protected worksheet. 默认值是 FalseThe default value is False.

注解Remarks

备注

以前的版本中,如果将此方法应用于将UserInterfaceOnly参数设置为True ,然后保存工作簿,则在重新打开工作簿时,整个工作表(而不仅仅是界面)将受到完全保护。In previous versions, if you apply this method with the UserInterfaceOnly argument set to True and then save the workbook, the entire worksheet (not just the interface) will be fully protected when you reopen the workbook. 若要在打开工作簿后重新启用用户界面保护,必须再次将此方法应用于UserInterfaceOnly设置为TrueTo re-enable the user interface protection after the workbook is opened, you must again apply this method with UserInterfaceOnly set to True.

如果要对受保护的工作表进行更改,如果提供了密码,则可以使用受保护的工作表上的protected 方法。If you want to make changes to a protected worksheet, it is possible to use the Protect method on a protected worksheet if the password is supplied. 此外,另一种方法是解除对工作表的保护,进行必要的更改,然后再次保护工作表。Also, another method would be to unprotect the worksheet, make the necessary changes, and then protect the worksheet again.

备注

不_受保护_意味着单元格可能已被锁定("设置单元格格式" 对话框),但包含在 "允许用户编辑区域" 对话框中定义的范围中,并且用户已通过 NT 权限对范围进行了不受保护的范围。Unprotected means that the cell may be locked (Format Cells dialog box) but is included in a range defined in the Allow Users to Edit Ranges dialog box, and the user has unprotected the range with a password or has been validated via NT permissions.

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.