Consulting

Results 1 to 3 of 3

Thread: Solved: Protect and unprotect worksheet change

  1. #1

    Solved: Protect and unprotect worksheet change

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim OtherSh As Worksheet
         
        On Error GoTo ws_exit
         
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Set OtherSh = ThisWorkbook.Sheets("Agent_View")
        If Target.Address = OtherSh.Range("D8").Address Then
            OtherSh.Range("H8").Value = ""
            'OtherSh.Unprotect = "54YY4F"
            OtherSh.Rows("18:47").ClearContents
            OtherSh.Rows("18:47").Interior.ColorIndex = 2
            'OtherSh.Protect = "54YY4F"
        ElseIf Target.Address = OtherSh.Range("H8").Address Then
            Run "Agent_View"
        ElseIf Target.Address = OtherSh.Range("C50").Address Then
            Run "Agent_View"
        End If
         
    ws_exit:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
         
    End Sub
    My code fails on the password protect and unprotect .. what have I done wrong ?

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    Ditch the equal sign from both. Also, you were trying to change the value of H8 one line too early.

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim OtherSh As Worksheet
         
        On Error GoTo ws_exit
         
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Set OtherSh = ThisWorkbook.Sheets("Agent_View")
        If Target.Address = OtherSh.Range("D8").Address Then
            OtherSh.Unprotect "54YY4F"
            OtherSh.Range("H8").Value = ""
            OtherSh.Rows("18:47").ClearContents
            OtherSh.Rows("18:47").Interior.ColorIndex = 2
            OtherSh.Protect "54YY4F"
        ElseIf Target.Address = OtherSh.Range("H8").Address Then
            Run "Agent_View"
        ElseIf Target.Address = OtherSh.Range("C50").Address Then
            Run "Agent_View"
        End If
         
    ws_exit:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
         
    End Sub

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For neatness and efficiency, make user of With

    [VBA]
    Set OtherSh = ThisWorkbook.Sheets("Agent_View")
    With OtherSh
    If Target.Address = .Range("D8").Address Then
    .Unprotect "54YY4F"
    .Range("H8").Value = ""
    .Rows("18:47").ClearContents
    .Rows("18:47").Interior.ColorIndex = 2
    .Protect "54YY4F"
    ElseIf Target.Address = .Range("H8").Address Then
    Run "Agent_View"
    ElseIf Target.Address = .Range("C50").Address Then
    Run "Agent_View"
    End If
    End With
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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