REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

When we are dealing with confidential information or data we need to Protect Workbook if you want from the user to edit data, delete data, and move data in Excel VBA. If user wants to see data in workbook user should have password to view data in the workbook. We need to protect workbook at the end of the procedure once changes has done to the workbook using VBA. If you forgot the password, you cannot unprotect the workbook.

VBA Protect Method Excel Workbook Object

Why we need to Protect a Workbook using VBA?

When workbook has sensitive data to deal with confidential information or data we need to Protect Workbook in Excel VBA. You should protect your complete workbook then only authorized users can view or modify the data.

VBA Protect Workbook – Syntax

Here is the syntax to Protect workbook using VBA.

Workbooks(“Your Workbook Name”).Protect(

[Password], [Structure], [Windows])

Where
Password: Its Optional argument. It specifies a case sensitive password for the workbook. If we want to protect workbook then you have to mention password argument, otherwise you can omit it. When you want to unprotect your protected workbook you must specify your password.
Structure: Its Optional parameter. The default value is False. If we mention it as True that means protect the workbook structure.
Windows: Its Optional parameter. The default value is False. If we mention it as True that means protect the workbook windows.
In the above syntax we are using ‘Protect’ method of workbook object to protect the workbook.

VBA Protect Workbook: Example 1

Please find the below example, It will show you how to protect the workbook.

Sub Protect_Sheet()
    Sheets("Sheet1").Protect "YourPassword", True, True
End Sub

In the above example we are protecting the workbook by using ‘Protect’ method of Worksheet object in the worksheet named ‘Sheet1’. When working workbook code has password protected, if you want to do any changes in the code first you have to unprotect the password at the beginning of the procedure and at the end of the procedure you have to protect it again.

VBA Protect Workbook: Example 2

Here is the example to protect ActiveWorkbook with password.

Sub Protect_Sheet()
    ActiveWorkbook.Protect Password:="YourPassword", Structure:=True, Windows:=True
End Sub

VBA Protect Workbook – Instructions

Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:

  1. Open an Excel Workbook
  2. Press Alt+F11 to Open VBA Editor
  3. Insert a Module from Insert Menu
  4. Copy the above code for activating a range and Paste in the code window(VBA Editor)
  5. Save the file as macro enabled workbook
  6. Press ‘F5’ to run it or Keep Pressing ‘F8’ to debug the code line by line.
Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Last Updated: March 2, 2023

One Comment

  1. Chris Bille January 13, 2019 at 6:22 PM - Reply

    I have three macros on my employees time card
    1. to clear the sheet
    2. to save the template sheet to a new tab with the current end of work date
    3. email the time card.

    How can I protect the workbook with structure so that the sheets cannot be deleted while still using the macros. I can currently protect only the sheet

Leave A Comment