Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Protect Or Unprotect Sheet Only If Password Matches

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0

    Protect Or Unprotect Sheet Only If Password Matches

    I`m in need of a code to protect and un-protect 2 sheets in a workbook.In the Activity Sheet I want the sheet protected except for the cells highlighted Green.These cell ranges are always available for editing so no protection needed.
    In the Progress sheet I want this whole sheet protected unless the Un-Protect button activated
    I will use a button on each sheet so as I can Protect or Unprotect each sheet individually....can these 2 individual buttons have password requirements
    Thanks
    Ray
    Attached Files Attached Files

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    raybugge, why don't you use the native sheet protection method, with password? And you can use separate passwords for each sheet. And you can use the button on the review tab (Excel 2007+) to protect or un-protect the file. I do not think you need a macro for this. Unless you have more elaborate plans
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    By the way, to leave a few cells as not protected, all you need is to change the property of the cells to Unlocked (default is Locked). Then protect the sheets.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    Hello Excel Fox
    I was going to use the native sheet protection but there will more cells locked then I have shown.This is a early version of a planned data spreadsheet...would prefer a code with button options if possible...thanks...Ray

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Try this. You'll need to paste this in the respective sheet module. Note the name of the command button. This has to be consistent with what's on your sheet. In your example above, it was CommandButton1

    Code:
    Private Sub CommandButton1_Click()
    
        Dim strPassword As String
        Const strActualPassword As String = "ABCD"
        strPassword = InputBox("Please enter the password", "Protect/Unprotect Sheet")
        
        If strActualPassword = strPassword Then
            If Me.CommandButton1.Caption = "PROTECT SHEET" Then
                Me.CommandButton1.Caption = "UNPROTECT SHEET"
                UnlockCells
                Me.Protect Password:=strPassword
            Else
                Me.CommandButton1.Caption = "PROTECT SHEET"
                Me.Unprotect Password:=strPassword
            End If
        Else
            MsgBox "Invalid Password"
        End If
        
    End Sub
    
    Sub UnlockCells()
    
        Me.Range("B10:T10,B16:T18,B24:T28").Locked = False
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    Hi Excel Fox

    I placed the code into "Activity" sheet and it works very good...can the code for the Sheet "Progress" be separate to the code for Activity sheet...use the same caption "Protect / Un-protect" on a commandbutton but this button either locks the whole sheet or unlocks the whole sheet

    The code for Activity sheet works independent of the code for Progress sheet...both sheets can be locked or unlocked at the same time,1 sheet locked while the other sheets is unlocked at the same time etc..etc..

    Thanks

    Ray
    Attached Files Attached Files

  7. #7
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Use the same code above, and just remove the function that unlocks the cells. Again, you can use any different password for this too.

    Code:
    Private Sub CommandButton1_Click()
    
        Dim strPassword As String
        Const strActualPassword As String = "ABCD"
        strPassword = InputBox("Please enter the password", "Protect/Unprotect Sheet")
        
        If strActualPassword = strPassword Then
            If Me.CommandButton1.Caption = "PROTECT SHEET" Then
                Me.CommandButton1.Caption = "UNPROTECT SHEET"
                Me.Protect Password:=strPassword
            Else
                Me.CommandButton1.Caption = "PROTECT SHEET"
                Me.Unprotect Password:=strPassword
            End If
        Else
            MsgBox "Invalid Password"
        End If
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  8. #8
    Junior Member
    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    Thanks

  9. #9
    Junior Member
    Join Date
    Jun 2013
    Posts
    16
    Rep Power
    0
    If I want to assign the code to a different type of button (insert rectangle shape) and not use a CommandButton what do I change in the code please
    Ray

  10. #10
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Just right click on the shape, click on Assign Macro, and select the sub-routine that you want to be assigned to that shape.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. VBA Code To Protect Sheet With Only A Few Cells Unlocked
    By rich_cirillo in forum Excel Help
    Replies: 3
    Last Post: 07-04-2013, 06:47 PM
  2. How do I get Vlookup to return multiple matches?
    By HANOOF in forum Excel Help
    Replies: 1
    Last Post: 06-04-2013, 10:06 PM
  3. Replies: 0
    Last Post: 04-20-2013, 10:07 AM
  4. Password Holder
    By littleiitin in forum Download Center
    Replies: 3
    Last Post: 01-01-2013, 03:22 PM
  5. CheckBox to see password
    By Ingolf in forum Excel Help
    Replies: 9
    Last Post: 08-26-2012, 11:56 PM

Posting Permissions

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