Excel宏,用于为行中的任何数据更改添加日期

时间:2017-01-31 12:46:23

标签: excel-vba vba excel

我希望解析特定Excel工作表中的整行,以了解该行中数据的任何更改。如果该行中的数据有任何变化,那么我想添加该行的特定单元格的日期。我想将行作为输入传递。我尝试了以下代码,但它不起作用。

Private Function User_func1(ByVal i As Long)

    Dim j As Long

    For j = 1 To j = 100
        If Cells(i, j).Value > 1 Then
            Cells(i, 2) = Now()
        End If
    Next j

End Function

1 个答案:

答案 0 :(得分:0)

您可以在要扫描的工作表中使用Worksheet_Change事件。

Option Explicit

Const RowtoTest = 2

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
If Target.Row = RowtoTest Then
    Target.Value = Date
End If
Application.EnableEvents = True

End Sub

选项2 :从某个单元格中获取要测试的行,让我们说单元格“A1”(值设置为2,表示在第2行中查找单元格中的更改)。

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
' compare the row number to the number inputted as the row to test in cell A1
If Target.Row = Range("A1").Value Then
    Target.Value = Date
End If
Application.EnableEvents = True

End Sub