Protect Method of Workbook Object VBA

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.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


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.
LIMITED TIME OFFER

Leave A Comment


Related pages


vlookup excel 2007excel advance filteractivex control excelcheck activexadvanced pivot tables excel 2007how to organize in alphabetical order in excelwriting vba codehow to hide a cell in excelhow to alphabetize rows in excelaccess parameter query vbavba case isexcel tutorial exampleshlookup formula with examplehow to remove duplicates in excel sheetexcel vba eventsaccess vba sql queryexcel macro message boxoption buttons in excelpowerpoint project status dashboard templatelearn excel for beginnersvba rs openexcel templates project managementvb length of arrayexcel formula substituteworksheetfunctionfree project costing template exceladd developer tab to the ribbon in word 2010worksheet protection excelaccess sql operatorsexcel combine data from multiple sheets into one sheetmultiple if statements in excel 2010delete range vbavba variable arrayshortcut key for change sheet in excelaccess 2007 vba tutorialsyntax of vlookupmacro recorder excel 2010excel 2013 remove password protectioncosting sheet template excelvisual basic calculator source codecombine data from multiple worksheets into onefree excel dashboard templates 2010unhide hidden columns3d excel chartvba excel pasteexcel plot chartworksheetfunction.sumsql technical interview questions and answerswith activesheethow to program vbavba display message boxmicrosoft excel sumif functionunprotect an excel workbook without passwordvba chr listexcel vba call subexcel insert row shortcut keyinsert button in excelproject update template ppthow to use sum if formulaexcel bar chart widthmicrosoft excel 2007 vbasurface charts in excelexcel macro tutorialsfind last row vbaworkbooks.open vbaexcel vba savecopyasexcel vba stringroot cause analysis template excelvlook up examplesexcel vba status bar progressms excel password protectvba workbook objectexcel programinginsert excel buttonvba in excel tutorialexcel password protect macroshortcut keys on excelvba inputvlookup in macroexcel check for nullheaders in excelhow to use autofit in excel