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?
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.
Wow!!! Don’t get intimidated by looking at the syntax. Have a look at the explanation of each argument below.
- Worksheet Name: First, we need to mention which worksheet we are going to protect.
- Password: We need to enter the password we are using to protect. If we ignore this parameter, excel will lock the sheet without a password, and while unprotecting the sheet, it will unprotect without asking for any password.
- Note: Remember the password you are giving, because if you forgot, then you have to go through various hard ways.
- Drawing Object: If you wish to protect objects in the worksheet, then you can pass the argument as TRUE or else FALSE. The default value is TRUE.
- Contents: To protect the contents of the worksheet, set the parameter as TRUE or else FALSE. The default value is FALSE. This will protect only locked cells. The default value is TRUE.
- Scenarios: If there are any what-if analysis in excel scenarios, we can protect them as well. To protect TRUE or else FALSE. The default value is TRUE.
- User Interface Only: If you want to protect the user interface other than macro, then it should be TRUE. If this argument is omitted, then it will protect both macros and user interface. If you set the argument to TRUE, it will protect only the user interface only. The default value is FALSE.
- Allow Formatting Cells: If you want to allow the user to format the cell, then you can set the parameter to TRUE or else FALSE. The default value is FALSE.
- Allow Formatting Columns: If you want to allow the user to format any column in the protected sheet, then you can set the parameter to TRUE or else FALSE. The default value is FALSE.
- Allow Formatting Rows: If you want to allow the user to format any row in the protected sheet, then you can set the parameter to TRUE or else FALSE. The default value is FALSE.
- Allow Insert Columns in VBA: you wish to allow the user to insert new columns, then you need to set this to TRUE. The default value is FALSE.
- Allow Insert Rows: If you wish to allow the user to insert new rows, then you need to set this to TRUE. The default value is FALSE.
- Allow Insert Hyperlinks: If you wish to allow the user to insert hyperlinks, then you need to set this to TRUE. The default value is FALSE.
- Allow Deleting Columns: If you wish to allow the user to delete columns in VBA, then you need to set this to TRUE. The default value is FALSE.
- Allow Deleting Rows: If you wish to allow the user to delete rows, then you need to set this to TRUE. The default value is FALSE.
- Allow Sorting: If you wish to allow the user to sort the data, then you need to set this to TRUE. The default value is FALSE.
- Allow Filtering: If you wish to allow the user to filter the data, then you need to set this to TRUE. The default value is FALSE.
- Allow Using Pivot Tables: If you wish to allow the user to use pivot tables, then you need to set this to TRUE. The default value is FALSE.
How to Protect Sheet using VBA Code?
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.

4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
For example, assume you want to protect the sheet named “Master Sheet,” then you need to mention the worksheet name like the below.
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
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
Now the variable “Ws” holds the reference of the worksheet named “Master Sheet.” By using this variable, we can access the IntelliSense list.
Step 4: Select Protect Method
Select the “Protect” method from the IntelliSense list.
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
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.
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