简体   繁体   中英

Excel macro to add date for any change in data in a row

I am looking to parse an entire row in a particular excel sheet for any change in data in that row. If there is any change in data in that row then i want to add the date in which that particular cell of that row. I want to pass the row as an input. I tried the following code but it doesnt work.


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

You can use the Worksheet_Change event in the sheet you want to scan.

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

Option 2 : Get the row to test from a certain cell, lets say Cell "A1" (value is set to 2, means look for changes in cells in row 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM