跟踪Excel单元格更改

时间:2012-12-13 16:32:46

标签: excel-vba excel-2003 vba excel

我想知道是否有人可以帮助我。

我正在使用下面的代码跟踪Excel单元格更改,并在“G”列中插入文本值“No”,在“A”列中插入单元格更改日期

Option Explicit
    Public preValue As Variant
    Private Sub Worksheet_Change(ByVal Target As Range)

        Dim Cell As Range
        If Target.Cells.Count > 1 Then Exit Sub
            On Error Resume Next
            If Not Intersect(Target, Range("B5:H10")) Is Nothing Then
            If Target.Value <> preValue And Target <> "" Then
            Application.EnableEvents = False
            Range("A" & Target.Row).Value = Date
            Range("G" & Target.Row).Value = "No"
            Application.EnableEvents = True
            Target.ClearComments
            Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "dd-mm-yyyy") & Chr(10) & "By " & Environ("UserName")
            Target.Interior.ColorIndex = 35
        End If
    End If
    On Error GoTo 0
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

        If Target.Count > 1 Then Exit Sub
        If Target = "" Then
            preValue = "a blank"
        Else: preValue = Target.Value
        End If
        preValue = Target.Value
    End Sub

我希望能够做的是进一步扩展这一点。因此,如果“G”列中的值从“否”变为“是”,我希望从列“B:G”中同一行的单元格中删除所有单元格着色,但我不知道如何这样做。

我只是想知道某人是否能够看到这个并提供一些关于如何改变这一点的指导。

非常感谢和亲切的问候

发布编辑 工作解决方案

Option Explicit
Public preValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim Rng As Range
    If Target.Cells.Count > 1 Then Exit Sub
    On Error Resume Next
    If Not Intersect(Target, Range("B5:W500")) Is Nothing Then
        If Target.Value <> preValue And Target.Value <> "" Then
            Application.EnableEvents = False
            Range("A" & Target.Row).Value = Date
            Range("AX" & Target.Row).Value = "No"
            Application.EnableEvents = True
            'Target.ClearComments
            'Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "dd-mm-yyyy") & Chr(10) & "By " & Environ("UserName")
            Target.Interior.ColorIndex = 35
        End If
    End If
    On Error GoTo 0
            If Target.Column = 50 Then
                If Target.Value = "Yes" Then
                Set Rng = Application.Union(Cells(ActiveCell.Row, "B").Resize(, 22), Cells(ActiveCell.Row, "W"))
                Rng.Interior.ColorIndex = xlNone
                End If
                End If
End Sub

1 个答案:

答案 0 :(得分:1)

所有人,在浏览互联网之后,我现在已经开始工作了。我在下面提供了我的解决方案。

Option Explicit
Public preValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    Dim Rng As Range
    If Target.Cells.Count > 1 Then Exit Sub
    On Error Resume Next
    If Not Intersect(Target, Range("B5:W500")) Is Nothing Then
        If Target.Value <> preValue And Target.Value <> "" Then
            Application.EnableEvents = False
            Range("A" & Target.Row).Value = Date
            Range("AX" & Target.Row).Value = "No"
            Application.EnableEvents = True
            'Target.ClearComments
            'Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "dd-mm-yyyy") & Chr(10) & "By " & Environ("UserName")
            Target.Interior.ColorIndex = 35
        End If
    End If
    On Error GoTo 0
            If Target.Column = 50 Then
                If Target.Value = "Yes" Then
                Set Rng = Application.Union(Cells(ActiveCell.Row, "B").Resize(, 22), Cells(ActiveCell.Row, "W"))
                Rng.Interior.ColorIndex = xlNone
                End If
                End If
End Sub