EXCEL;在受保护的工作表上更新另一个单元格后更改单元格

时间:2013-02-08 19:32:04

标签: excel excel-vba vba

编辑:如果我使用ActiveSheet.unprotect,则有效

好的,如果我的工作表不受保护,以下工作正常!

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Reset_EnableEvents
Application.EnableEvents = False


If Not Intersect(Target, Range("D6:G6")) Is Nothing Then 'do the following if D6 is updated
Range("D7").Interior.ColorIndex = 15
    If Range("D6") = "No" Then
        Range("D7").Interior.ColorIndex = 38
    End If
End If

If Not Intersect(Target, Range("D7:G7")) Is Nothing Then 'do the following if D6 is updated
    If Range("D7") <> "" Then
        Range("D7").Interior.ColorIndex = 15
    End If
End If

If Not Intersect(Target, Range("e12:f12")) Is Nothing Then 'do the following if E12:F12 is updated

    Range("d28").Value = Range("e12").Value 'set d28 to date entered in e12
    Range("c23").Interior.ColorIndex = 36 ' reset color of cell c23


    If Range("e12") = "" Then ' do the following if e12 is empty
        Range("c39") = Chr(34) & "Do you work period?" & Chr(34)
    Else
        Range("c39") = Chr(34) & "Do you work beyond " & Range("e13").Text & " (approx. period)?" & Chr(34)

            If Date >= Range("e15").Value Then
                Range("c23").Interior.ColorIndex = 3
            End If
    End If

End If

If Not Intersect(Target, Range("E32")) Is Nothing Then 'do the following if E32 is updated
    If Range("e32") = "Yes" Then
        MsgBox "Please have the client complete a MVA Questionnaire."
    End If
End If

If Not Intersect(Target, Range("E41")) Is Nothing Then 'do the following if E41 is updated
    If Range("e41") = "Yes" Then
        Range("d42").Interior.ColorIndex = 38
    ElseIf Range("e41") = "No" Then
        Range("d42").Interior.ColorIndex = 36
        Range("d42") = "Not Required"
    ElseIf Range("e41") = "" Then
        Range("d42").Interior.ColorIndex = 36
        Range("d42") = ""
    End If
End If



Reset_EnableEvents:
Application.EnableEvents = True
End Sub

但是当我保护工作表并且只允许选择未锁定的单元格并向工作表添加密码时,上述单元格背景颜色和单元格值不会更新

我尝试在开头和结尾添加activeworksheet.unprotect并保护它仍然无法正常工作!

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Reset_EnableEvents
Application.EnableEvents = False

'ActiveWorkSheet.Unprotect Password:="a"

`..... all the if not intersect

ActiveWorkSheet.Protect Password:="a"

Reset_EnableEvents:
Application.EnableEvents = True
End Sub 

我还尝试在每个if语句中加上保护和取消保护,但它仍然无效,

If Not Intersect(Target, Range("e12:f12")) Is Nothing Then 'do the following if E12:F12 is updated
ActiveWorkSheet.Unprotect Password:="a"
    Range("d28").Value = Range("e12").Value 'set d28 to date entered in e12
    Range("c23").Interior.ColorIndex = 36 ' reset color of cell c23


    If Range("e12") = "" Then ' do the following if e12 is empty
        Range("c39") = Chr(34) & "Do you work period?" & Chr(34)
    Else
        Range("c39") = Chr(34) & "Do you work beyond " & Range("e13").Text & " (approx. period)?" & Chr(34)

            If Date >= Range("e15").Value Then
                Range("c23").Interior.ColorIndex = 3
            End If
    End If
ActiveWorkSheet.Protect Password:="a"
End If

帮助??

1 个答案:

答案 0 :(得分:0)

我将其更改为ActiveSheet.Protect并且有效