• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Not able to un protect worksheet manually, protected by code

ThrottleWorks

Excel Ninja
Hi,

I am using below 2 lines to protect and un protect a worksheet.

MacroSht.protect Password = "Yamaha"
MacroSht.Unprotect Password = "Yamaha"

These lines are working perfect without any problems. At end of code, I protect the sheet ( by code only).

However when I try to un protect this sheet manually by entering same password.
I get an error pop up as incorrect password.

I am not able to understand the cause. Can anyone please help me in this.
 
Hi @chirayu , thanks a lot for the help, won't be possible to share the exact file. However even if I try same code on a new book, it's giving me same problem.

Code:
Sub test()
Worksheets("Sheet1").Unprotect Password = "Yamaha"
Worksheets("Sheet1").Protect Password = "Yamaha"
End Sub
Have a nice day ahead. :)
 
Last edited by a moderator:
Change code to this. Reason its doing that is because you didn't use semicolon
Code:
Sub test()
Worksheets("Sheet1").Unprotect Password:= "Yamaha"
Worksheets("Sheet1").Protect Password:= "Yamaha"
End Sub
 
Last edited by a moderator:
Hi @chirayu , thanks for the help. However, change in code is giving me bug as Run Time Error 91 Object Variable or With Block variable not set.

Could you please help if you get time.
 
Your current password is "False" without the quotes (or whatever the equivalent in your Excel version's language is). Your code should be, as already said:

Code:
MacroSht.Protect Password:="Yamaha"
MacroSht.Unprotect Password:="Yamaha"
but you will need to first unprotect the sheet using False.
 
Hi @Debaser , thanks for help. I am trying this. Also, I will try to upload sample file. This is taking too much time of experts who are helping me. Good night. :)
 
No need for a sample file. ;)

In VBA, this:
Code:
Password = "Yamaha"
means "is the variable Password equal to the string Yamaha?". Since you (probably) haven't declared a variable called Password, it will not equal Yamaha, so the expression evaluates to False.

Since the first argument for Unprotect and Protect is a string (the password), the compiler converts the Boolean False to the text "False" and supplies that as the password.

So the first thing you need to do is unprotect your worksheet using the password "False" without the quotes.

Then change your code as suggested, and you should be fine.
 
Back
Top