Skip to main content
A newer version of this page is available. .

How to: Protect a Workbook

  • 2 minutes to read

The example below demonstrates how to protect an entire workbook by using the Workbook.Protect method. Workbook protection prevents end-users from modifying a workbook structure (by moving, deleting, adding, renaming, or hiding existing worksheets, or displaying hidden sheets).

Dim worksheet As Worksheet = workbook.Worksheets("ProtectionSample")
workbook.Worksheets.ActiveWorksheet = worksheet

' Protect workbook structure with the password (prevent users from adding or 
'deleting worksheets or displaying hidden worksheets).
If Not workbook.IsProtected Then
    workbook.Protect("password", True, False)
End If
' Add a note.
worksheet("B2").Value = "Workbook structure is protected with a password. " & ControlChars.Lf & " You cannot add, move or delete worksheets until protection is removed."
worksheet.Visible = True

Unprotect a workbook

To remove protection, use the Workbook.Unprotect method.

Dim worksheet As Worksheet = workbook.Worksheets("ProtectionSample")
workbook.Worksheets.ActiveWorksheet = worksheet

' Unprotect the workbook using a password.
If workbook.IsProtected Then
    workbook.Unprotect("password")
End If
' Add a note.
worksheet("B2").Value = "Workbook is unprotected. Workheets can be added, moved or deleted."
worksheet.Visible = True
See Also