Worksheet_Change多个事件

时间:2018-02-01 15:37:53

标签: excel excel-vba vba

我对这个主题Worksheet_Change很新。我想把这3个事件放在一张纸上。
有人可以帮我解决这个问题吗?

第一个和第二个只给出了不同单元格中的日期和用户名 第二个在写入内容后阻止所有单元格。我已经尝试了所有......

代码1:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rC As Range
If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each rC In Target.Cells
Range("F" & rC.Row) = Now()
Range("G" & rC.Row) = Environ("username")
Next rC
Application.EnableEvents = True
End Sub

代码2:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim P2 As Range
If Intersect(Target, Range("C:C")) Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each P2 In Target.Cells
Range("H" & P2.Row) = Now()
Range("I" & P2.Row) = Environ("username")
Next P2
Application.EnableEvents = True
End Sub

代码3:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range
ActiveSheet.Unprotect Password:="Athens"
For Each cel In Target
If cel.Value <> "" Then
cel.Locked = True
End If
Next cel
ActiveSheet.Protect Password:="Athens"
End Sub

2 个答案:

答案 0 :(得分:0)

喜欢这样吗?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rC As Range
    Dim P2 As Range
    Dim cel As Range

    If Not Intersect(Target, Range("B:B")) Is Nothing Then
        Application.EnableEvents = False
        For Each rC In Target.Cells
            Range("F" & rC.Row) = Now()
            Range("G" & rC.Row) = Environ("username")
        Next rC
        Application.EnableEvents = True
    ElseIf Not Intersect(Target, Range("C:C")) Is Nothing Then
        Application.EnableEvents = False
        For Each P2 In Target.Cells
            Range("H" & P2.Row) = Now()
            Range("I" & P2.Row) = Environ("username")
        Next P2
    Else
        Application.EnableEvents = True
        ActiveSheet.Unprotect Password:="Athens"
        For Each cel In Target
            If cel.Value <> "" Then
                cel.Locked = True
            End If
        Next cel
        ActiveSheet.Protect Password:="Athens"
    End If
End Sub

答案 1 :(得分:-1)

需要更改它以使其按预期工作。我想阻止所有通过直接交互修饰的细胞。感谢帮助!没有你的帮助,我无法做到这一点

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim rC As Range
 Dim P2 As Range
 Dim cel As Range


    Application.EnableEvents = True
    ActiveSheet.Unprotect Password:="Athens"
    For Each cel In Target
        If cel.Value <> "" Then
            cel.Locked = True
            End If
    Next cel
    ActiveSheet.Protect Password:="Athens"

If Not Intersect(Target, Range("B:B")) Is Nothing Then
    Application.EnableEvents = False
    For Each rC In Target.Cells
        Range("F" & rC.Row) = Now()
        Range("G" & rC.Row) = Environ("username")
    Next rC
    Application.EnableEvents = True
ElseIf Not Intersect(Target, Range("C:C")) Is Nothing Then
    Application.EnableEvents = False
    For Each P2 In Target.Cells
        Range("H" & P2.Row) = Now()
        Range("I" & P2.Row) = Environ("username")
    Next P2
        Application.EnableEvents = True
End If

End Sub