+ Reply to Thread
Results 1 to 5 of 5

Protect/Unprotect all sheets in a different file

  1. #1
    Registered User
    Join Date
    02-23-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Protect/Unprotect all sheets in a different file

    Hi,

    I have found code to protect/unprotect all sheets using a command buttion and textboxes but I'd like to keep the code in a separate file from the file which I am trying to protect/unprotect sheets. I'd like to be able to use this code for any file.

    Also, when password-protecting sheets, I'd like the code to request a confirmation of the password.

    Below is the current code:

    Private Sub CommandButton1_Click()

    Dim wSheet As Worksheet

    For Each wSheet In Worksheets

    If wSheet.ProtectContents = True Then

    wSheet.Unprotect Password:=TextBox1.Text

    Else

    wSheet.Protect Password:=TextBox1.Text

    End If

    Next wSheet

    Unload Me

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Protect/Unprotect all sheets in a different file

    Hi there,

    First of all, you should note that your code will not "protect/unprotect ALL worksheets in a workbook", what it will actually do is to TOGGLE the existing Protected/Unprotected state of each individual worksheet. For example, if Sheets 1 & 2 are protected and Sheets 3 & 4 are unprotected, your code will unprotect Sheets 1 & 2 and PROTECT Sheets 3 & 4. I don't think this is what you intend.

    You need some reference for determining whether all sheets are to be PROtected or UNprotected, and the code in the attached workbook checks the protected/unprotected state of the ACTIVE worksheet, and applies the inverse of that state to all sheets in the workbook.

    The code acts upon all sheets contained in whatever workbook is currently active.

    When worksheets are being protected, a "Confirm Password" textbox is displayed. If the password entered in this textbox is different from that in the "Enter Password" textbox, an error message is displayed and code execution pauses until either the correct password is entered or the entire operation is cancelled. When worksheets are being unprotected, no "Confirm Password" textbox is displayed as no "Confirm" operation is required.

    I hope the above and the attached workbook are of some help. Please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files
    Last edited by Greg M; 02-23-2011 at 11:46 AM.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Protect/Unprotect all sheets in a different file

    Deleted - unintended duplicate of previous post
    Last edited by Greg M; 02-23-2011 at 11:46 AM. Reason: Post deleted - I accidentally posted it twice!

  4. #4
    Registered User
    Join Date
    02-23-2011
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Protect/Unprotect all sheets in a different file

    Hi Greg,

    This solutions is brilliant and exactly what I needed. Thank you so much.

    Kind regards,

    Robert

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Protect/Unprotect all sheets in a different file

    Hi Robert,

    Many thanks for your feedback - I'm glad I was able to help.

    Regards,

    Greg M

+ Reply to Thread

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