In this Excel VBA Protect Sheet with Password Tutorial, you learn how to protect a sheet with password using Excel macros.
This Excel VBA Protect Sheet with Password Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process below. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Protect Sheet with Password Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section below.
Table of Contents
Related Excel Macro and VBA Training Materials and Resources
The following Excel Macro and VBA Tutorials may help you better understand and implement the contents below.
- Tutorials about general macro and VBA constructs and structures:
- Tutorials for Beginners:
- Excel Macros: Click here to open.
- Excel VBA: Click here to open.
- Enable macros in Excel: Click here to open.
- Work with the Visual Basic Editor (VBE): Click here to open.
- Create Sub procedures: Click here to open.
- Refer to objects (click here to open), including sheets (click here to open).
- Work with:
- Methods: Click here to open.
- Data types: Click here to open.
- Loops: Click here to open.
- Tutorials for Beginners:
- Tutorials with practical VBA applications and macro examples:
- Activate workbook: Click here to open.
- Create new workbook: Click here to open.
- Open workbook: Click here to open.
- Delete sheet: Click here to open.
This Excel VBA Protect Sheet with Password Tutorial is part of a more comprehensive series of Excel VBA Protect or Unprotect Sheet Tutorials.
- Excel VBA Protect Sheet Without Password in 2 Easy Steps: Click here to open.
- Excel VBA Unprotect Sheet Without Password in 2 Easy Steps: Click here to open.
- Excel VBA Unprotect Sheet with Password in 2 Easy Steps: Click here to open.
- Excel VBA Protect Sheet Allow Filter in 2 Easy Steps: Click here to open.
- Excel VBA Protect Sheet Allow Select Locked Cells in 4 Easy Steps: Click here to open.
You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives.
If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:
- Premium Courses at the Power Spreadsheets Academy: Click here to open.
- Books at the Power Spreadsheets Library: Click here to open.
- VBA Cheat Sheets: Click here to open.
If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:
- Code generators.
- An extensive code library.
- The ability to create your own code library.
- Advanced coding tools.
If you need consulting services, you may want to consider working with ExcelRescue. ExcelRescue is my usual suggestion for people who (like you) may need help with Excel tasks/projects: Click here to visit ExcelRescue (affiliate link).
The VBA Protect Sheet with Password Snippet Template/Structure
The following is the VBA protect sheet with password snippet template/structure I explain (step-by-step) in the Sections below.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
'Sheet is a worksheet
WorkbookObjectReference.WorksheetObjectReference.Protect Password:="SheetPassword"
'Sheet is a chart sheet
WorkbookObjectReference.ChartObjectReference.Protect Password:="SheetPassword"
The Example Before VBA Protect Sheet with Password
This Excel VBA Protect Sheet with Password Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process below. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Protect Sheet with Password Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section above.
The example workbook has a single (empty) worksheet.
- The name of the example worksheet is “Protect Sheet with Password”.
- This is the sheet the VBA protect sheet with password example macro I create (by following the step-by-step process below) works with. In other words: The VBA protect sheet with password example macro protects this sheet with a password.
The image below displays the example worksheet before I execute the VBA protect sheet with password example macro.
Notice the Protect Sheet button (inside the Protect group of commands) in the Excel Ribbon (indicating the example worksheet isn't (currently) protected).
Step 1: Refer to Sheet
Refer to the sheet you want to protect with password.
In other words: Create a VBA expression that returns an object representing the applicable sheet (you want to protect with password). As a general rule: Work with 1 of the following objects:
- A Worksheet object, representing a worksheet.
- A Chart object, representing a chart sheet.
Consider explicitly including the following references to create a fully qualified object reference returning the applicable Worksheet (representing a worksheet) or Chart (representing a chart sheet) object:
- A reference to the applicable workbook. The following VBA constructs (among others) may return a Workbook object:
- The Application.ThisWorkbook property.
- The Application.Workbooks and Workbooks.Item properties.
- The Application.ActiveWorkbook property.
- A reference to the applicable worksheet or chart sheet. The following VBA constructs (among others) may return a Worksheet or Chart object:
- The Workbook.Sheets and Sheets.Item properties.
- The Workbook.Worksheets and Worksheets.Item properties.
- The Workbook.Charts and Charts.Item properties.
- The Application.ActiveSheet property.
- The Workbook.ActiveChart property.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
'Sheet is a worksheet
WorkbookObjectReference.WorksheetObjectReference
'Sheet is a chart sheet
WorkbookObjectReference.ChartObjectReference
Step 1 Example
I:
- Refer to the worksheet named “Protect Sheet with Password” inside the workbook where the procedure is stored.
- Work with the following VBA constructs to obtain a Worksheet object representing this worksheet:
- The Application.ThisWorkbook property: ThisWorkbook.
- The Workbook.Worksheets and Worksheets.Item properties: Worksheets(“Protect Sheet with Password”).
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
ThisWorkbook.Worksheets("Protect Sheet with Password")
Step 2: Protect Sheet and Specify Password
Do the following to:
- Protect the sheet (you refer to in step #1); and
- Specify the protection password.
(1) Start with the Worksheet or Chart object reference you created in step #1.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
'Sheet is a worksheet
WorkbookObjectReference.WorksheetObjectReference
'Sheet is a chart sheet
WorkbookObjectReference.ChartObjectReference
(2) Call the applicable version of the Protect method:
- Worksheet.Protect, if protecting a worksheet.
- Chart.Protect, if protecting a chart sheet.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
'Sheet is a worksheet
WorkbookObjectReference.WorksheetObjectReference.Protect
'Sheet is a chart sheet
WorkbookObjectReference.ChartObjectReference.Protect
(3) Use the Password parameter of the Protect method to specify the protection password.
- Specify the password as a string.
- The password you specify is case sensitive.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
'Sheet is a worksheet
WorkbookObjectReference.WorksheetObjectReference.Protect Password:="SheetPassword"
'Sheet is a chart sheet
WorkbookObjectReference.ChartObjectReference.Protect Password:="SheetPassword"
Step 2 Example
I do the following:
(1) Start with the Worksheet object reference I created in step #1.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
ThisWorkbook.Worksheets("Protect Sheet with Password")
(2) Call the Worksheet.Protect method.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
ThisWorkbook.Worksheets("Protect Sheet with Password").Protect
(3) Use the Password parameter of the Worksheet.Protect method to specify the protection password as “Protect Sheet with Password”.
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
ThisWorkbook.Worksheets("Protect Sheet with Password").Protect Password:="Protect Sheet with Password"
The full VBA protect sheet with password example macro is as follows:
Sub ProtectSheetPassword()
'Source: https://powerspreadsheets.com/
'More information: https://powerspreadsheets.com/vba-protect-sheet-password/
'Do the following:
'Step 1: Refer to the "Protect Sheet with Password" worksheet in this workbook
'Step 2: Protect the worksheet with the password "Protect Sheet with Password"
ThisWorkbook.Worksheets("Protect Sheet with Password").Protect Password:="Protect Sheet with Password"
End Sub
The GIF below illustrates the effects of using the VBA protect sheet with password example macro.
Notice how:
- The Protect Sheet button (inside the Protect group of commands) in the Excel Ribbon (indicating the example worksheet isn't (currently) protected);
- Is replaced by the Unprotect Sheet button (indicating the example worksheet is now protected) when I execute the VBA protect sheet with password example macro.
Download the VBA Protect Sheet with Password Example Workbook
This Excel VBA Protect Sheet with Password Tutorial is accompanied by an Excel workbook with the data and VBA code I use when describing the step-by-step process above. Get this example workbook (for free) by clicking the button below.
The VBA code in the Excel workbook that accompanies this Excel VBA Protect Sheet with Password Tutorial is (always) stored in the Visual Basic Editor (VBE). If you don't know how to work with the VBE, I suggest you read my Visual Basic Editor (VBE) Tutorial. I link to this Tutorial in the Related Excel Macro and VBA Training Materials and Resources Section above.
Related Excel Macro and VBA Training Materials and Resources
The following Excel Macro and VBA Tutorials may help you better understand and implement the contents above.
- Tutorials about general macro and VBA constructs and structures:
- Tutorials for Beginners:
- Excel Macros: Click here to open.
- Excel VBA: Click here to open.
- Enable macros in Excel: Click here to open.
- Work with the Visual Basic Editor (VBE): Click here to open.
- Create Sub procedures: Click here to open.
- Refer to objects (click here to open), including sheets (click here to open).
- Work with:
- Methods: Click here to open.
- Data types: Click here to open.
- Loops: Click here to open.
- Tutorials for Beginners:
- Tutorials with practical VBA applications and macro examples:
- Activate workbook: Click here to open.
- Create new workbook: Click here to open.
- Open workbook: Click here to open.
- Delete sheet: Click here to open.
This Excel VBA Protect Sheet with Password Tutorial is part of a more comprehensive series of Excel VBA Protect or Unprotect Sheet Tutorials.
- Excel VBA Protect Sheet Without Password in 2 Easy Steps: Click here to open.
- Excel VBA Unprotect Sheet Without Password in 2 Easy Steps: Click here to open.
- Excel VBA Unprotect Sheet with Password in 2 Easy Steps: Click here to open.
- Excel VBA Protect Sheet Allow Filter in 2 Easy Steps: Click here to open.
- Excel VBA Protect Sheet Allow Select Locked Cells in 4 Easy Steps: Click here to open.
You can find more Excel and VBA Tutorials in the organized Tutorials Archive: Click here to visit the Archives.
If you want to learn how to automate Excel (and save time) by working with macros and VBA, you may be interested in the following Premium Excel Macro and VBA Training Materials:
- Premium Courses at the Power Spreadsheets Academy: Click here to open.
- Books at the Power Spreadsheets Library: Click here to open.
- VBA Cheat Sheets: Click here to open.
If you want to save time when working with macros and VBA, you may be interested in AutoMacro: Click here to learn more about AutoMacro (affiliate link). AutoMacro is an add-in for VBA that installs directly into the VBE. Depending on the version, AutoMacro comes loaded with:
- Code generators.
- An extensive code library.
- The ability to create your own code library.
- Advanced coding tools.
If you need consulting services, you may want to consider working with ExcelRescue. ExcelRescue is my usual suggestion for people who (like you) may need help with Excel tasks/projects: Click here to visit ExcelRescue (affiliate link).