Macro to Protect workbook using a password

P

PCLIVE

I know this would be a little unorthodox, but I currently have a macro that
will unprotect a workbook including a password. The line is as follows:

ActiveWorkbook.Unprotect (password)

I also want a macro that will do just the opposite, including the password.
Is this possible? I've tried the following with no luck.

ActiveWorkbook.Protect (password), Structure:=True, Windows:=True
 
J

J.E. McGimpsey

What does "no luck" mean?

Your code works fine for me, though there's no need for the
parentheses around the password.
 
K

Ken Macksey

Hi

This works for me. Just make sure to set the takefocusonclick property to
false if using a command button.

Private Sub CommandButton1_Click()

' Protect button

For i = 1 To Sheets.Count
ActiveWorkbook.Sheets(i).Protect "your password"
Next i

ActiveWorkbook.Protect "your password", Structure:=True, Windows:=True

End Sub



Private Sub CommandButton2_Click()

' unprotect button

ActiveWorkbook.Unprotect "your password"


For i = 1 To Worksheets.Count
ActiveWorkbook.Worksheets(i).Unprotect "your password"
Next i



HTH

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top