• Excel worksheets: How to know whether any has password?

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Excel worksheets: How to know whether any has password?

    Author
    Topic
    #493534

    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.

    Viewing 1 reply thread
    Author
    Replies
    • #1440950

      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
      

      36325-ProtectionStatus

      HTH :cheers:

      Sample file: 36326-VBA-Excel-Check-for-PW-Protected-Sheets

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1440973

        Thank you very much. I’m not clear on the comment “Try to unprotect”. What code is trying to unprotect the sheet?

        • #1440975

          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 :cheers:

          May the Forces of good computing be with you!

          RG

          PowerShell & VBA Rule!
          Computer Specs

          • #1440979

            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.

    • #1441274

      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).

    Viewing 1 reply thread
    Reply To: Excel worksheets: How to know whether any has password?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: