+ Reply to Thread
Results 1 to 7 of 7

Protecting and unprotecting a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    01-08-2010
    Location
    Montréal Québec
    MS-Off Ver
    Excel 2003
    Posts
    9

    Protecting and unprotecting a cell

    Hi,

    My little loop checks if the value in a cell equals a number. At first, it hides or unhides rows if it equals the number I am looking for(Works Perfect). It then checks another colums to see if it equals another number I am looking for, and it "should" protect or unprotect another cell in the same row(the part where I have problems with).

    Here is my code:
    Sub HURows()
        BeginRow = 1
        EndRow = 181
        ChkCol = 16
        ChkCol2 = 18
        Password = "TI9779"
    
        For RowCnt = BeginRow To EndRow
                If Cells(RowCnt, ChkCol).Value = 1 Then
                    Cells(RowCnt, ChkCol).EntireRow.Hidden = True
                Else
                    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
                End If
                If Cells(RowCnt, ChkCol2).Value = 1 Then
                    Sheet2.Unprotect Password
                    Range("$G(RowCnt)").Locked = True
                    Sheet2.Protect Password, False, False, False, False, False, False, True
                                    
                    If Cells(RowCnt, ChkCol2).Value = 2 Then
                        Sheet2.Unprotect Password
                        Range("$G(RowCnt)").Locked = False
                        Sheet2.Protect Password, False, False, False, False, False, False, True
                    End If
                End If
    
        Next RowCnt
    End Sub
    Last edited by royUK; 01-08-2010 at 01:13 PM. Reason: add code tags

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Problem with protecting and unprotecting a cell

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Added this time
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Problem with protecting and unprotecting a cell

    Try this
    
    Sub HURows()
        BeginRow = 1
        EndRow = 181
        ChkCol = 16
        ChkCol2 = 18
        Password = "TI9779"
        With Sheet2
            For RowCnt = BeginRow To EndRow
                If .Cells(RowCnt, ChkCol).Value = 1 Then
                    .Cells(RowCnt, ChkCol).EntireRow.Hidden = True
                Else
                    .Cells(RowCnt, ChkCol).EntireRow.Hidden = False
                End If
                If .Cells(RowCnt, ChkCol2).Value = 1 Then
                    .Unprotect Password
                    .Cells(RowCnt, 7).Locked = True
                    .Protect Password
                    If .Cells(RowCnt, ChkCol2).Value = 2 Then
                        .Unprotect Password
                        Cells(RowCnt, 7).Locked = False
                        .Protect Password
                    End If
                End If
    
            Next RowCnt
            End With
        End Sub
    Last edited by royUK; 01-08-2010 at 01:49 PM. Reason: amend code

  4. #4
    Registered User
    Join Date
    01-08-2010
    Location
    Montréal Québec
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Problem with protecting and unprotecting a cell

    Seems like it is not protecting my worksheet back, I need the worksheet to be protected after change and allow user to Format rows. So I think that this line is not correct
    .Protect Password, False, False, False, False, False, False, True

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Problem with protecting and unprotecting a cell

    I've changed the code,try the new code

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Problem with protecting and unprotecting a cell

    Just noticed that none of your variables are declared, & Passowrd may be a word that Excel reserves so that you cannot use it as a variable.
    Option Explicit
    
    Sub HURows()
    Dim beginRow As Long
    Dim endRow As Long
    Dim chkCol As Long
    Dim chkCol2 As Long
    Dim RowCnt As Long
    Const sPassword As String = "TI9779"
        beginRow = 1
        endRow = 181
        chkCol = 16
        chkCol2 = 18
        With Sheet2
            For RowCnt = beginRow To endRow
                If .Cells(RowCnt, chkCol).Value = 1 Then
                    .Cells(RowCnt, chkCol).EntireRow.Hidden = True
                Else
                    .Cells(RowCnt, chkCol).EntireRow.Hidden = False
                End If
                If .Cells(RowCnt, chkCol2).Value = 1 Then
                    .Unprotect sPassword
                    .Cells(RowCnt, 7).Locked = True
                    .Protect sPassword
                    If .Cells(RowCnt, chkCol2).Value = 2 Then
                        .Unprotect sPassword
                        Cells(RowCnt, 7).Locked = False
                        .Protect "Password, False, False, False, False, False, False, True"
                    End If
                End If
    
            Next RowCnt
            End With
        End Sub

  7. #7
    Registered User
    Join Date
    01-08-2010
    Location
    Montréal Québec
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Protecting and unprotecting a cell

    The code works, except for the password portion. When I remove the code about the password, it protects and unprotects correctly, but when I add the password lines, it does not work.

+ 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