This is part of a larger Excel VBA project that I am working on. The sheet is protected, but I need it to be unprotected so that my VBA code can access it. I obviously want it to be protected afterwards again.
The answer is to use the Unprotect
and Protect
methods of the Worksheet object:
Sheets("NAMES").Unprotect "password" ' do lots of stuff to the worksheet... ' while it is unprotected... ' and then re-protect it: Sheets("NAMES").Protect "password"
Of course, anyone could just view the VBA code to see the password, but at least at this stage, no accidental errors can be made.
Read the post Password-protecting your VBA Project to learn how to prevent users from being able to discover your password (or your Kung Fu code!) by viewing the VBA Project.
References:
- VBA Protect / Unprotect Worksheets – Automate Excel. (2022). Retrieved 6 October 2022, from https://www.automateexcel.com/vba/unprotect-protect-worksheets/#Unprotect_Excel_Worksheet_With_Password