Description:

Protect and UnProtect Worksheets in Excel VBA is useful when we work with the templates or some worksheets which structure will not change but data may change. Or we may want to restrict the users to not to modify the structure of the worksheet.

Protect UnProtect Worksheet in Excel VBA – Solution:

Protect UnProtect Worksheet in Excel VBA
We can use Protect and UnProtect Methods of a Worksheet to Protect and UnProtect Worksheets in Excel using VBA.

Protect UnProtect Worksheet in Excel VBA – Examples:

The following example will show you how to protect and unprotect worksheets in Excel using VBA.

Example to Protect Worksheet
Sub sbProtectSheet()
ActiveSheet.Protect "password", True, True
End Sub

Here the left side part is the sheet which you want to protect. the first parameter will be password: you an provide any password to protect worksheet.

Example to UnProtect Worksheet
Sub sbUnProtectSheet()
ActiveSheet.Unprotect "password"
End Sub

Here the leftside part is the worksheet which you want to un-protect and the right side is the password to unprotect it which you have provided while protecting the worksheet.

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a Module for Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. Press F5 to execute it

Protect All Worksheets in Workbook

We can Protect All Worksheets in Workbook using VBA. If you want to restrict all worksheets from users not to modify the structure, we can protect all worksheets.

Protect All Worksheets in Workbook using VBA – Solution:

Protect All Worksheets in Workbook using VBA
We can use Protect method and iterate all the worksheets in a workbook.

Protect All Worksheets in Workbook using VBA – Exmaple:

Follwoing is the example code will show you how to Protect All Worksheets in Workbook using VBA.

Code
Sub sbProtectAllSheets()
Dim pwd1 As String, pwd2 As String
pwd1 = InputBox("Please Enter the password")
If pwd1 = "" Then Exit Sub
pwd2 = InputBox("Please re-enter the password")
If pwd2 = "" Then Exit Sub
'Check if both the passwords are identical
If InStr(1, pwd2, pwd1, 0) = 0 Or _
InStr(1, pwd1, pwd2, 0) = 0 Then
MsgBox "You entered different passwords. No action taken"
Exit Sub
End If
For Each ws In Worksheets
ws.Protect Password:=pwd1
Next
MsgBox "All sheets Protected."
Exit Sub
End Sub
Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a Module for Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. Press F5 to execute it

UnProtect All Worksheets in Workbook using VBA

We can UnProtect All Worksheets in Workbook using VBA. If you want to delete the restrictions of all worksheets, we can unprotect all worksheets at a time.

UnProtect All Worksheets in Workbook using VBA- Solution:

UnProtect All Worksheets in Workbook using VBA We can use Unprotect method and iterate all the worksheets in a workbook.

UnProtect All Worksheets in Workbook using VBA – Example:

Following is the example code will show you how to UnProtect All Worksheets in Workbook using VBA.

Code
Sub sbUnProtectAll()
On Error GoTo ErrorOccured
Dim pwd1 As String
pwd1 = InputBox("Please Enter the password")
If pwd1 = "" Then Exit Sub
For Each ws In Worksheets        
ws.Unprotect Password:=pwd1
Next
MsgBox "All sheets UnProtected."
Exit Sub
ErrorOccured:
MsgBox "Sheets could not be UnProtected - Password Incorrect"
Exit Sub
End Sub
Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a Module for Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. Press F5 to execute it

Show Worksheet Protect dialog in Excel VBA

You can Show Worksheet Protect dialog to give the user to enter password to protect worksheets. So that user can have the option to enter required password.

Show Worksheet Protect dialog – Solution:

Show Worksheet Protect dialog in Excel VBA
We can use Application.Dialogs(xlDialogProtectDocument).Show method to Show Worksheet Protect dialog.

Show Worksheet Protect dialog – Example:

Following is the example to Show you how to do this.

Code
Sub sbShowProtectDialogBox()
'This code will do the required task...
Application.Dialogs(xlDialogProtectDocument).Show
End Sub
Instructions:

Follow the instructions below to execute the code.

  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a Module for Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. Press F5 to execute it

Hey! Join Our Community

Get Quick Responses & Experts' Answers in Minutes!
Get Notified - When Answered Your Question!