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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Sheets("NAMES").Unprotect "password"
' do lots of stuff to the worksheet...
' while it is unprotected...
' and then re-protect it:
Sheets("NAMES").Protect "password"
Sheets("NAMES").Unprotect "password" ' do lots of stuff to the worksheet... ' while it is unprotected... ' and then re-protect it: Sheets("NAMES").Protect "password"
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:

  1. 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

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.