HELP!!!!

Scruffymessiah

New Member
Joined
Sep 5, 2002
Messages
45
I have been trying to unprotect a workbook by assigning the following to a button. The top code is for protecting the lower one for unprotecting. The top works and the bottom throws up errors. Can anyone help? I am new to VBA.

Sub Protect()
Dim theWord As String
theWord = InputBox("Please enter the password")
If theWord <> "lock" Then
MsgBox ("Sorry, you have entered the wrong password"), vbCritical
Exit Sub
End If


ActiveWorkbook.Protect Password:="lock", Structure:=True, Windows:=False
ActiveSheet.Protect Password:="lock", DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub


Sub unprotect_sheets()
Dim theWord As String
theWord = InputBox("Please enter the password")
If theWord <> "unlock" Then
MsgBox ("Sorry, you have entered the wrong password"), vbCritical
Exit Sub
End If


ActiveWorkbook.unprotect_sheets Password:="unlock", Structure:=False, Windows:=True
ActiveSheet.unprotect_sheets Password:="unlock", DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You have different passwords for protecting and unprotecting and they need to be the same.
 
Upvote 0
Well if it's protected with the word "lock" it needs to be unprotected with the word "lock", not "unlock".
 
Upvote 0
I just tried making them the same but to no avail. The error states that:

"Run-time error 438
Object does not support this property or method"

?
 
Upvote 0
Here's what I have now:

Sub Unprotect()
Dim theWord As String
theWord = InputBox("Please enter the password")
If theWord <> "lock" Then
MsgBox ("Sorry, you have entered the wrong password"), vbCritical
Exit Sub
End If


ActiveWorkbook.Unprotect Password:="lock", Structure:=True, Windows:=False
ActiveSheet.Unprotect Password:="lock", DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub


The error now is:

Compile error.
Named argument not found.

It seems to refer to:"Structure:="

Sorry about this.....I will buy a book on VBA...promise!!
 
Upvote 0
Unlike Protect, Unprotect has only one argument (Password). So:

ActiveWorkbook.Unprotect Password:="lock"
ActiveSheet.Unprotect Password:="lock"

No need for a book. Just use Help - put the cursor on the word in your code and press F1.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top