简体   繁体   中英

Create an automatic date stamp in excel from an entry

I am trying to have a date stamp event happen in column B when an entry is made in column A. Now i can do this in VBA with no problem, the trouble i am running into is there is also a entry that will eventually go in say column D and would need a date stamp in column E as well. is this possible. here is a sample of the code i have used so far.

Private Sub Worksheet_Change(ByVal Target As Range) For Each Cell In Target If Cell.Column <= 3 Then If Cells(Cell.Row, 1) <> "" Then Cells(Cell.Row, 2) = Now End If Next Cell End Sub

If you're OK with every odd column being entered by a user, and the timestamps being in the even columns (ie You can tyep in Column A, and the TimeStamp will go in Column B. You can type in Column C and the timestamp will go in Column D, etc.) then you can use this:

Private Sub Worksheet_Change(ByVal Target As Range)

    'Only write a timestamp of an odd column changes (because the timestamps go in the even columns)
    If Target.Column Mod 2 > 0 Then

        'Get the first part of the address, to get the actual column being changed
        Dim columnAddress As String
        columnAddress = Target.Address

        If InStr(columnAddress, ":") > 0 Then
            columnAddress = Left(columnAddress, InStr(columnAddress, ":") - 1)
        End If

    'This will cause the TimeStamp to be undeletable (kind of like na Audit).
    'If you want the timestamp to disappear when you clear the column, uncomment the next few lines:

    '        If Not ActiveSheet.Range(columnAddress).Formula = "" Then

            ''Write the timestamp for the previous column
            ActiveSheet.Range(columnAddress).Offset(0, 1).Formula = Now

    '        Else
    '            ActiveSheet.Range(columnAddress).Offset(0, 1).Formula = ""
    '        End If
    End If


End Sub

You can hide columns where you don't need the Timestamp to show.

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