+ Reply to Thread
Results 1 to 6 of 6

Lock and Unlock Sheets based on Cell values

Hybrid View

Mujahid_Sgd Lock and Unlock Sheets based... 01-31-2011, 05:52 AM
DonkeyOte Re: Lock and Unlock Sheets... 01-31-2011, 10:32 AM
Mujahid_Sgd Re: Lock and Unlock Sheets... 02-01-2011, 05:11 AM
Mujahid_Sgd Re: Lock and Unlock Sheets... 02-01-2011, 05:19 AM
DonkeyOte Re: Lock and Unlock Sheets... 02-01-2011, 05:21 AM
Mujahid_Sgd Re: Lock and Unlock Sheets... 02-02-2011, 05:18 AM
  1. #1
    Registered User
    Join Date
    01-31-2011
    Location
    PAKISTAN
    MS-Off Ver
    Excel 2007
    Posts
    4

    Lock and Unlock Sheets based on Cell values

    Hi All,

    I have an excel workbook in which there are three sheets, each sheet has two cells which I have named as Sheet1!Sign1, Sheet1!Sign2.... Sheet3!Sign2.

    What I intend to do is to lock the sheet whenever the signature cells are filled in for a particular sheet.

    Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
    
        
    Dim NamesList
    Dim x As Integer
    Dim SignStatus1, SignStatus2 As Boolean
        
    For x = 1 To ActiveWorkbook.Worksheets.Count
        For y = 1 To ActiveWorkbook.Worksheets(x).Names.Count
            NamesList = ActiveWorkbook.Worksheets(x).Names(y).Name
            If InStr(1, NamesList, "Sign1", vbTextCompare) <> 0 And ActiveWorkbook.Worksheets(x).Names(NamesList).Value = 0 Then
                    
                    SignStatus1 = False
            Else
                   'ActiveWorkbook.Worksheets(x).Range(NamesList).Locked = False
                    SignStatus1 = True
            End If
            
            If InStr(1, NamesList, "Sign2", vbTextCompare) <> 0 And ActiveWorkbook.Worksheets(x).Names(NamesList).Value = 0 Then
                    SignStatus2 = False
            Else
                    'ActiveWorkbook.Worksheets(x).Range(NamesList).Locked = False
                    SignStatus2 = True
            End If
        Next y
        
            If SignStatus1 = True And SignStatus2 = True Then
                ActiveWorkbook.Worksheets(x).Protect
            Else
                ActiveWorkbook.Worksheets(x).Unprotect
            End If
    Next x
    The overall intention is that when someone fills in the signature cells the sheet gets locked or protected and whenever the signs are deleted only then data can be edited

    With the code above the sheet gets protected but I want to leave the sign cells unprotected.

    Two commented lines in the code return an error

    Unable to set the locked property of range class to locked

    Can someone please help

    Thanks

    Mujahid

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Lock and Unlock Sheets based on Cell values

    If you need to be able to clear Sign1 & Sign2 then those cells must always remain Unlocked irrespective of sheet protection - on which basis perhaps you want something like:

    Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
        Dim rngS As Range
        Const C_PWD = "Password"
        Select Case UCase(sh.Name)
            Case "SHEET1", "SHEET2", "SHEET3"
                sh.Unprotect C_PWD
                Set rngS = Union(sh.Range("Sign1"), sh.Range("Sign2"))
                rngS.Locked = False
                If Not Intersect(Target, rngS) Is Nothing Then
                    If Application.CountA(rngS) = rngS.Cells.Count Then sh.Protect C_PWD
                End If
                Set rngS = Nothing
        End Select
    End Sub
    so the above will always leave Sign1 & Sign2 unlocked and will protect the sheet only where both Sign1 & Sign2 have been populated.

    modify variables / references as necessary

  3. #3
    Registered User
    Join Date
    01-31-2011
    Location
    PAKISTAN
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Lock and Unlock Sheets based on Cell values

    Thanks for the reply..
    and thanks for the help.. the code works beautifully..

    Just wondering if this code can be extended to n number of sheets.

  4. #4
    Registered User
    Join Date
    01-31-2011
    Location
    PAKISTAN
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Lock and Unlock Sheets based on Cell values

    I tried this and it seems to be working

    Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
        Dim rngS As Range
        Const C_PWD = "locked"
        Select Case (sh.Index)
            Case 1 To ActiveWorkbook.Worksheets.Count
                sh.Unprotect C_PWD
                Set rngS = Union(sh.Range("Sign1"), sh.Range("Sign2"))
                rngS.Locked = False
                If Not Intersect(Target, rngS) Is Nothing Then
                    If Application.CountA(rngS) = rngS.Cells.Count Then sh.Protect C_PWD
                End If
                Set rngS = Nothing
        End Select
    End Sub

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Lock and Unlock Sheets based on Cell values

    Quote Originally Posted by Mujahid_Sgd
    Just wondering if this code can be extended to n number of sheets.
    if "n" is determined by simply however many sheets have both Sign1 & Sign2 ranges then it might be simplest to make this requirement the pre-emptive test:

    Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
        Dim rngS As Range
        Const C_PWD = "Password"
        If [ISREF(Sign1)+ISREF(Sign2)] = 2 Then
            sh.Unprotect C_PWD
            Set rngS = Union(sh.Range("Sign1"), sh.Range("Sign2"))
            rngS.Locked = False
            If Not Intersect(Target, rngS) Is Nothing Then
                If Application.CountA(rngS) = rngS.Cells.Count Then sh.Protect C_PWD
            End If
            Set rngS = Nothing
        End If
    End Sub
    or

    Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
        Dim rngS As Range
        Const C_PWD = "Password"
        On Error Resume Next
        Set rngS = Union(Range("Sign1"), Range("Sign2"))
        On Error GoTo 0
        If Not rngS Is Nothing Then
            sh.Unprotect C_PWD
            rngS.Locked = False
            If Not Intersect(Target, rngS) Is Nothing Then
                If Application.CountA(rngS) = rngS.Cells.Count Then sh.Protect C_PWD
            End If
            Set rngS = Nothing
        End If
    End Sub
    Last edited by DonkeyOte; 02-01-2011 at 05:23 AM.

  6. #6
    Registered User
    Join Date
    01-31-2011
    Location
    PAKISTAN
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Lock and Unlock Sheets based on Cell values

    Can the same functionality by put in an add in so that I donot have to write the code behind everywork book.
    I actually tried saving the book as an add in and then enabled that add in but it does not work even if I change the function type to public and define Sign1 and Sign2 in the other workbook

    What am I doing wrong?

+ 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