WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Protect Sheet

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.

  • 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?

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.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

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 –

  • VBA Do Loop
  • Rename Sheet in VBA
  • Activate a Sheet with VBA Code
  • Use WorksheetFunction in VBA
6 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA Protect Sheet Excel Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More