VBA Protect Sheet

Excel VBA Protecting Sheet

We can protect the excel sheet using vba code which doesn’t allow the user to make any changes to the worksheet data, all they can do is just to read the report. For this, we have a built-in vba method called “Protect”.

Like we protect our worksheets in excel similarly we can use VBA to protect our worksheets, it is done by using a .protect statement, there are two methods to protect sheet one is with a password, and another is without password, the syntax to protect a worksheet is as follows Worksheets().Protect Password.

We usually share the final end report with the user or reader. When we share the final end report with the user, we wish the user will not make any modification or manipulate the end report. In such a scenario, it is all about trust, isn’t it?

VBA-Protect-Sheet

Syntax

The protecting sheet involves various parameters to supply. This is unlike Unprotecting the sheet. Let’s look at the syntax of the Protect method with a password.

VBA Protect password formula

Wow!!! Don’t get intimidated by looking at the syntax. Have a look at the explanation of each argument below.

How to Protect Sheet using VBA Code?

You can download this VBA Protect Sheet Excel Template here – VBA Protect Sheet Excel Template

Step 1: Select Sheet which needs to be protected

To protect the sheet, the first step is to decide upon which sheet we need to protect using a password, and we need to call the sheet by its name by using the VBA Worksheet Object.

For example, assume you want to protect the sheet named “Master Sheet,” then you need to mention the worksheet name like the below.

VBA Protect Sheet Example 1

Step 2: Define Worksheet Variable

After mentioning the worksheet name, put a dot, but we don’t see any IntelliSense list to work with. This makes the job difficult. To get access to the IntelliSense list, defines the variable as a worksheet.

Code:

Sub Protect_Example1()

  Dim Ws As Worksheet

End Sub
VBA Protect Sheet Example 1-1

Step 3: Give Worksheet Reference

Now set the worksheet reference to the variable as Worksheets(“Master Sheet”).

Code:

Sub Protect_Example1()

   Dim Ws As Worksheet

   Set Ws = Worksheets("Master Sheet")

End Sub
VBA Protect Sheet Example 1-2

Now the variable “Ws” holds the reference of the worksheet named “Master Sheet.” By using this variable, we can access the IntelliSense list.

Example 1-3

Step 4: Select Protect Method

Select the “Protect” method from the IntelliSense list.

Example 1-4

Step 5: Enter Password

Specify the password in double-quotes.

Code:

Sub Protect_Example1()

   Dim Ws As Worksheet

   Set Ws = Worksheets("Master Sheet")

   Ws.Protect Password:="MyPassword"

End Sub
Example 1-5

Step 6: Run the Code

Run the code manually or using the shortcut key F5 then, it will protect the sheet named as “Master Sheet.”

When the sheet is protected, if we want to do any modification, then it shows some error message, as shown below.

VBA Protect Sheet Example 1-6

In case if you wish to protect more than one sheet, then we need to use loops. Below is the example code to protect the sheet.

Sub Protect_Example2()

  Dim Ws As Worksheet

  For Each Ws In ActiveWorkbook.Worksheets
  Ws.Protect Password:="My Passw0rd"
  Next Ws

End Sub

Note: Use other parameters to experiment.

Recommended Articles

This has been a guide to VBA Protect Sheet. Here we learn how to use protect methods in VBA to protect or lock an excel sheet using a password along with a practical example and a downloadable template. Below you can find some useful excel VBA articles –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>