Protect and Unprotect using Macros

bradjedis
4StarLounger
Posts: 538
Joined: 30 Mar 2010, 18:49
Location: United States

Protect and Unprotect using Macros

Post by bradjedis »

Hello,
I have gotten this far, but need a little more help. I have 2 buttons 1 to protect a sheet, and 1 to unprotect a sheet. I have recorded the macros, but I want the macro to request a password for each.

what is the next step?

Excel 2007.

Thanks, Brad



Sub Protect()
'
' Macro2 Macro
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

End Sub


Sub Unprotect()
'
' Macro3 Macro

ActiveSheet.Unprotect


End Sub

User avatar
HansV
Administrator
Posts: 78523
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Protect and Unprotect using Macros

Post by HansV »

Code: Select all

Sub Protect()
  Dim strPassword As String
  strPassword = InputBox("Please provide the password")
  ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=strPassword
End Sub

Sub Unprotect()
  Dim strPassword As String
  strPassword = InputBox("Please provide the password")
  On Error GoTo ErrHandler
  ActiveSheet.Unprotect Password:=strPassword
  Exit Sub
ErrHandler:
  MsgBox "Wrong password!", vbExclamation
End Sub
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2631
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Protect and Unprotect using Macros

Post by jstevens »

Here is an example of masking the password with **********

A sample file is attached along with the code in the form.

Regards,
John
You do not have the required permissions to view the files attached to this post.
Regards,
John

bradjedis
4StarLounger
Posts: 538
Joined: 30 Mar 2010, 18:49
Location: United States

Re: Protect and Unprotect using Macros

Post by bradjedis »

way cool for both inputs....


Thanks much,
Brad