Joppe
Joppe

Reputation: 45

Excel (2003) - Automatic insert date on a cell

I don't really know if this is a programming question, but I am sure one of you can easily help me with this one.

I am trying to create a automatic "inserted date" function inside excel. i.e. When a person inputs data in a row in my excel document I want another cell to automatically show the date of insertion.

Standing inside the cell i am trying to show the date, I've written the following:

=IF(ISBLANK(C20);1;TODAY())

This works great, until I open it the day after. Clearly it will set the date to "TODAY", but if I want it to only update once, at the time of the insertion - how would I do that?

Thinking something like this (Java - pseudo).

IF(!OTHER.CELL.ISBLANK() && THIS.CELL.ISBLANK()){
   THIS.CELL = TODAY();
}

Now, how to do that in Excel?

Thanks in advance.

Upvotes: 2

Views: 9417

Answers (1)

brettdj
brettdj

Reputation: 55672

You would use the Worksheet_Change Event

  1. Right click your sheet tab
  2. View - Code
  3. Copy and Paste in the code below

This code

  • tracks and change made to column C of the Activesheet
  • puts in the current data and user logon name to each corresponding cell in column D

Only changed column C cells are captured as specified in this line Set rng1 = Intersect(Range("C:C"), Target)

The Application.EnableEvents = False is used to stop the code refiring when column D is writing to

You could easily adapt this to
1) write to a different (perhaps hidden) log sheet
2) write to a text file instead

Pls let me know if you want any updates

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Set rng1 = Intersect(Range("C:C"), Target)
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
rng1.Offset(0, 1).Value = Now() & " - " & Environ("username")
Application.EnableEvents = True
End Sub

enter image description here

Upvotes: 7

Related Questions