vba protect unprotect worksheet in Excel

Q199. In Excel, how can I protect / unprotect WorkSheet using VBA?

To do it in Excel, here is the answer:

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


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 do I use Find to determine last occurrence of a string in a WorkSheet range using VBA?

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

3) How can I prevent users from seeing / accessing my macro code?

4) How can I add a WorkSheet and name it as required using VBA?

5) How can I loop through all ActiveX checkboxes in WorkSheet and set them to Unchecked status?

6) How can I extract file name from a full path including folder path and file name?

7) How can I identify the cells that are dependent on a particular cell?

8) How can I export a WorkSheet as a PDF using VBA?

9) How do I add a shape using VBA?

10) How can I avoid updates to cell values during macro execution?

11) How can I copy and rename a WorkSheet using VBA?

12) I have WorkBook with sensitive confidential information - how do I encrypt / protect my file so that access to file contents is restricted?

13) How can worksheet functions be accessed in VBA?

14) How can I list all files in a folder using VBA?

15) How can I update a listbox based on data in a list using VBA?