0
votes

I know of the 2 possible ways to password-protect sheets in Excel -

  1. Manual Password protect
  2. Password protect using VBA

This is where I'm stumped. I have password-protected a sheet using VBA and then tried to unprotect it manually with the same password used in VBA. But Excel throws an error saying that the password provided is incorrect. I tried the opposite as well - Protected the file manually and then tried to unprotect using VBA providing the right password in the code; same error. Any ideas why this is happening?

2

2 Answers

1
votes

After some analysis, I figured out what the issue is in my code -

ActiveWorkbook.Protect Password = "xyz", Structure:=True, Windows:=False

The password option was assigned with an "=" sign instead of a ":=". This code still protects the sheet but with an empty password. When I use a similar Unprotect code like,

ActiveWorkbook.Unprotect Password = "xyz"

the unprotect code is passing on an empty password. Hence, if I try a manual password of "xyz" it throws an error.

The correct code that solves the issue is

ActiveWorkbook.Protect Password:= "xyz", Structure:=True, Windows:=False
ActiveWorkbook.Unprotect Password:= "xyz
0
votes

If I am understanding your question correctly, it happens because each password is protecting something different. They are not the same thing. You can have a workbook that is not password protected but the VBA code is password protected.