Partager via


_Worksheet.Protect Method

Protects a worksheet so that it cannot be modified.

Namespace:  Microsoft.Office.Interop.Excel
Assembly:  Microsoft.Office.Interop.Excel (in Microsoft.Office.Interop.Excel.dll)

Syntax

'Declaration
Sub Protect ( _
    Password As Object, _
    DrawingObjects As Object, _
    Contents As Object, _
    Scenarios As Object, _
    UserInterfaceOnly As Object, _
    AllowFormattingCells As Object, _
    AllowFormattingColumns As Object, _
    AllowFormattingRows As Object, _
    AllowInsertingColumns As Object, _
    AllowInsertingRows As Object, _
    AllowInsertingHyperlinks As Object, _
    AllowDeletingColumns As Object, _
    AllowDeletingRows As Object, _
    AllowSorting As Object, _
    AllowFiltering As Object, _
    AllowUsingPivotTables As Object _
)
'Usage
Dim instance As _Worksheet
Dim Password As Object
Dim DrawingObjects As Object
Dim Contents As Object
Dim Scenarios As Object
Dim UserInterfaceOnly As Object
Dim AllowFormattingCells As Object
Dim AllowFormattingColumns As Object
Dim AllowFormattingRows As Object
Dim AllowInsertingColumns As Object
Dim AllowInsertingRows As Object
Dim AllowInsertingHyperlinks As Object
Dim AllowDeletingColumns As Object
Dim AllowDeletingRows As Object
Dim AllowSorting As Object
Dim AllowFiltering As Object
Dim AllowUsingPivotTables As Object

instance.Protect(Password, DrawingObjects, _
    Contents, Scenarios, UserInterfaceOnly, _
    AllowFormattingCells, AllowFormattingColumns, _
    AllowFormattingRows, AllowInsertingColumns, _
    AllowInsertingRows, AllowInsertingHyperlinks, _
    AllowDeletingColumns, AllowDeletingRows, _
    AllowSorting, AllowFiltering, AllowUsingPivotTables)
void Protect(
    Object Password,
    Object DrawingObjects,
    Object Contents,
    Object Scenarios,
    Object UserInterfaceOnly,
    Object AllowFormattingCells,
    Object AllowFormattingColumns,
    Object AllowFormattingRows,
    Object AllowInsertingColumns,
    Object AllowInsertingRows,
    Object AllowInsertingHyperlinks,
    Object AllowDeletingColumns,
    Object AllowDeletingRows,
    Object AllowSorting,
    Object AllowFiltering,
    Object AllowUsingPivotTables
)

Parameters

  • Password
    Type: System.Object

    Optional Object. 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. It's a good idea to keep a list of your passwords and their corresponding document names in a safe place.

  • DrawingObjects
    Type: System.Object

    Optional Object. True to protect shapes. The default value is False.

  • Contents
    Type: System.Object

    Optional Object. True to protect contents. For a chart, this protects the entire chart. For a worksheet, this protects the locked cells. The default value is True.

  • Scenarios
    Type: System.Object

    Optional Object. True to protect scenarios. This argument is valid only for worksheets. The default value is True.

  • UserInterfaceOnly
    Type: System.Object

    Optional Object. True to protect the user interface but not macros. If this argument is omitted, protection applies both to macros and to the user interface.

  • AllowFormattingCells
    Type: System.Object

    Optional Object. True allows the user to format any cell on a protected worksheet. The default value is False.

  • AllowFormattingColumns
    Type: System.Object

    Optional Object. True allows the user to format any column on a protected worksheet. The default value is False.

  • AllowFormattingRows
    Type: System.Object

    Optional Object. True allows the user to format any row on a protected worksheet. The default value is False.

  • AllowInsertingColumns
    Type: System.Object

    Optional Object. True allows the user to insert columns on the protected worksheet. The default value is False.

  • AllowInsertingRows
    Type: System.Object

    Optional Object. True allows the user to insert rows on the protected worksheet. The default value is False.

  • AllowInsertingHyperlinks
    Type: System.Object

    Optional Object. True allows the user to insert hyperlinks on the worksheet. The default value is False.

  • AllowDeletingColumns
    Type: System.Object

    Optional Object. True allows the user to delete columns on the protected worksheet, where every cell in the column to be deleted is unlocked. The default value is False.

  • AllowDeletingRows
    Type: System.Object

    Optional Object. True allows the user to delete rows on the protected worksheet where every cell in the row to be deleted is unlocked. The default value is False.

  • AllowSorting
    Type: System.Object

    Optional Object. True allows the user to sort on the protected worksheet. Every cell in the sort range must be unlocked or unprotected. The default value is False.

  • AllowFiltering
    Type: System.Object

    Optional Object. 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. The default value is False.

  • AllowUsingPivotTables
    Type: System.Object

    Optional Object. True allows the user to use pivot table reports on the protected worksheet. The default value is False.

Remarks

If you apply the Protect method with the UserInterfaceOnly argument set to True to a worksheet and then save the workbook, the entire worksheet (not just the interface) will be fully protected when you reopen the workbook. To re-enable the user interface protection after the workbook is opened, you must again apply the Protect method with UserInterfaceOnly set to True.

If you want to make a change to a protected worksheet, it is possible to use the Protect method on a protected worksheet if the password is supplied. Another method would be to unprotect the worksheet, make the necessary changes, and then protect the worksheet again.

Note

'Unprotected' means the cell may be locked (Format Cells dialog) but is included in a range defined in the Allow Users to Edit Ranges dialog, and the user has unprotected the range with a password or been validated via NT permissions.

See Also

Reference

_Worksheet Interface

_Worksheet Members

Microsoft.Office.Interop.Excel Namespace