Results 1 to 6 of 6
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    853
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Excel worksheets: How to know whether any has password?

    Using VBA, is there a way to determine whether any of the worksheets in ActiveWorkbook are protected with a password? I'm familiar with ActiveWorkbook.HasPassword, but I want to determine if ANY of the sheets within the workbook have a password.

    Thanks.

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,785
    Thanks
    184
    Thanked 699 Times in 637 Posts
    Richard,

    See if this meets your needs.
    Code:
    Sub Check4PW()
    
       Dim shtCurr    As Worksheet
       Dim zMsg       As String
       
       Application.ScreenUpdating = False
    
       For Each shtCurr In ActiveWorkbook.Sheets
       
          If shtCurr.ProtectContents Then
            On Error Resume Next
            shtCurr.Protect Password:="", Contents:=False  '*** Try to unprotect ***
            If Err = 1004 Then    '*** Sheet is PW protected report ***
              zMsg = zMsg & shtCurr.Name & " is Password Protected" & vbCrLf
            Else   '*** Sheet Protected but no PW reprotect! ***
              zMsg = zMsg & shtCurr.Name & " is Protected w/o a Password" & vbCrLf
              shtCurr.Protect Contents:=True
            End If   'Err = 1004
            
            On Error GoTo 0    'Kill error trap
          Else      '*** No Protection ***
            zMsg = zMsg & shtCurr.Name & " is not Protected" & vbCrLf
          End If
          
       Next shtCurr
       
               MsgBox zMsg, vbOKOnly + vbInformation, "Protected Sheet Password Status:"
      
    End Sub   'Check4PW
    ProtectionStatus.JPG

    HTH

    Sample file: VBA - Excel - Check for PW Protected Sheets.xlsm
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  4. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    853
    Thanks
    0
    Thanked 9 Times in 8 Posts
    Thank you very much. I'm not clear on the comment "Try to unprotect". What code is trying to unprotect the sheet?

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    5,785
    Thanks
    184
    Thanked 699 Times in 637 Posts
    Quote Originally Posted by richardbarrett View Post
    Thank you very much. I'm not clear on the comment "Try to unprotect". What code is trying to unprotect the sheet?
    Richard,

    The line with the comment is trying to unprotect the Content [Content:=False] using a blank password [Password:=""]. At the point this statement executes the code has already determined that the sheet is protected. The only way to determine if it has a PW is to try to unprotect it using a blank password which will fail if there is a password with an error code of 1004. This is then trapped and the message generated. If no error occurs the sheet is protected but without a PW. Since the statement will unprotect this sheet the Else clause reprotects the sheet to leave it as we found it.

    HTH
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  6. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    853
    Thanks
    0
    Thanked 9 Times in 8 Posts
    Thanks for the clarification. I see that "ProtectConents" is the property I'm looking at. So in my case, as soon as I find a sheet in the collection where that proves True, I'll know that there's a protect sheet and can ExitFor. All I want to know is whether there is at least one protected sheet, password is not critical.
    Thanks again.

  7. #6
    New Lounger
    Join Date
    Dec 2009
    Location
    Fort Myers, FL
    Posts
    17
    Thanks
    0
    Thanked 2 Times in 2 Posts
    This may be a bit tangential, but ANY document, spreadsheet, or other material I want encrypted, I encrypt with the same, separate program-7-zip, in my case, but there are many other good programs out there. I don't use any internal encryptation on any Office or other programs. That way, I have one program, and one password, to encrypt and decrypt any sensitive material. Were I to upload it to the cloud, I would encrypt it before uploading. It makes management much easier. BTW, when I do my monthly offsite backup to an external HD i keep in a safe deposit box, I also decrypt the encrypted files on a separate flash drive, and make a copy- one in the safe deposit box, and one for the home safe- in case of sudden death and the need of anyone managing my estate to have access to these documents rapidly, even though I also have the password key on paper in my box and safe.

    BTW, I also use a password manager, and NEVER store passwords within my browser(s).

Posting Permissions

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