Vba protect unprotect worksheet in Excel

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Sub SetPasswordForWorkSheet()
  3. ActiveSheet.Protect Password:="ExamplePassword"
  4. End Sub

Description:

a) Line 3 - hardcodes the password. One option is to get user nput for Password using InputBox

b) To unprotect WorkSheet, the command becomes, ActiveSheet.UnProtect Password:="ExamplePassword"

 

You can find similar Excel Questions and Answer hereunder

1) How can I add a legend to a chart using VBA?

2) How can I check if a file exists in a folder using VBA?

3) How can I identify all cells with Conditional Formatting in my WorkSheet?

4) Vba list all files in a folder in Excel

5) How can I get users to select a folder to save the output of my macro?

6) How to print a worksheet in Excel VBA

7) How can I set Page orientation, Zoom % , Title Rows and footer using VBA?

8) The scenario manager in Excel VBA allows to explore various scenarios in a very easy way

9) How can I clear cell after activating a routine when there is a change in value of a cell?

10) How can I add Trendline to a chart using VBA?

 

Here the previous and next chapter