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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,123
You have different passwords for protecting and unprotecting and they need to be the same.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Well if it's protected with the word "lock" it needs to be unprotected with the word "lock", not "unlock".
 

Scruffymessiah

New Member
Joined
Sep 5, 2002
Messages
45
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"

?
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092

ADVERTISEMENT

unprotect_sheets should be Unprotect.
 

Scruffymessiah

New Member
Joined
Sep 5, 2002
Messages
45
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!!
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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.
 

Forum statistics

Threads
1,144,119
Messages
5,722,592
Members
422,447
Latest member
knopp

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
Top