Results 1 to 1 of 1

Macro Protect & Unprotect Multiple Worksheets & Workbook (Structure) With same Password

Threaded View

  1. #1
    Registered User
    Join Date
    01-15-2013
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2010
    Posts
    44

    Macro Protect & Unprotect Multiple Worksheets & Workbook (Structure) With same Password

    Hi,

    I am new to Macros and have only recently started recording little ones.
    I don't understand the language of them very well and as such trawled Google and found this one on a site from many years ago.
    It promises to do what I need - i.e. Protect & unprotect Multiple Worksheets AND Workbook Structure with a lot less clicks than I would need if doing it manully.
    I have tested it, but it doesn't seem to work just so - It protects everything but it allows me to unprotect the workbook without the use of a password. I did change the Password - but have taken it back to how I found it for the forum.

    Could someone help me - I just want to Protect ALL Worksheets - Protect All Workbook (Structure) - Unprotect All Workbook (Structure) - Unprotect ALL Worksheets. 4 Steps - Passwords required to UnProtect (Both Stages) Oh and use the same password.

    Thanks in Advance.

    Dim ws As Worksheet 
    Sub ProtectAll() 
        Dim S As Object 
        Dim pWord1 As String, pWord2 As String 
        pWord1 = InputBox("Please Enter the password") 
        If pWord1 = "" Then Exit Sub 
        pWord2 = InputBox("Please re-enter the password") 
         
        If pWord2 = "" Then Exit Sub 
         'make certain passwords are identical
        If InStr(1, pWord2, pWord1, 0) = 0 Or _ 
        InStr(1, pWord1, pWord2, 0) = 0 Then 
            MsgBox "You entered different passwords. No action taken" 
            Exit Sub 
        End If 
        For Each ws In Worksheets 
            ws.Protect Password:=pWord1 
        Next 
        MsgBox "All sheets Protected." 
        Exit Sub 
         
    End Sub

    Sub UnProtectAll() 
        Dim S As Object 
        Dim pWord3 As String 
        pWord3 = InputBox("Please Enter the password") 
        If pWord3 = "" Then Exit Sub 
        For Each ws In Worksheets 
            On Error Goto errorTrap1 
            ws.Unprotect Password:=pWord3 
        Next 
        MsgBox "All sheets UnProtected." 
        Exit Sub 
         
    errorTrap1: 
        MsgBox "Sheets could not be UnProtected - Password Incorrect" 
        Exit Sub 
         
         
    End Sub

    Sub ProtectWorkbook() 
        Dim S As Object 
        Dim pWord3 As String, ShtName As String 
        pWord5 = InputBox("Please Enter the password") 
        If pWord5 = "" Then Exit Sub 
        ShtName = "Workbook as a whole" 
        On Error Goto errorTrap1 
        ActiveWorkbook.Protect Structure:=True, Windows:=False, Password:=pWord5 
        MsgBox "The workbook's structure has been protected." 
        Exit Sub 
         
    errorTrap1: 
        MsgBox "Workbook could not be Protected" 
        Exit Sub 
         
         
    End Sub

    Sub UnProtectWorkbook() 
        Dim S As Object 
        Dim pWord3 As String, ShtName As String 
        pWord5 = InputBox("Please Enter the password") 
        If pWord5 = "" Then Exit Sub 
        ShtName = "Workbook as a whole" 
        On Error Goto errorTrap1 
        ActiveWorkbook.Unprotect Password:=pWord5 
        MsgBox "The workbook's structure has been Unprotected." 
        Exit Sub 
         
    errorTrap1: 
        MsgBox "Workbook could not be UnProtected - Password Incorrect" 
        Exit Sub 
         
         
    End Sub
    Last edited by arlu1201; 03-12-2013 at 04:44 AM. Reason: Split code snippets

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1