tags:

views:

64

answers:

1

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

+1  A: 

If you're OK with every odd column being entered by a user, and the timestamps being in the even columns (i.e. 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.

Michael Rodrigues
That will work. Is there any way that when a range for both A and B are selected and deleted it doesn't act funny?
Obfus
Fixed!If it works for you, please remember to mark this answer as accepted!
Michael Rodrigues
Thanks! It works great!
Obfus