VBA Date and Time Stamp Issue

JoRyBar

New Member
Joined
Aug 19, 2018
Messages
17
Hello Gurus,

I am currently working on adding a date/time stamp when things are entered into a column. So for example, if I enter something in column O, I would like the date/time stamp in column P. This following code seems to work most of the time, but once in a while, it stops working and I get a debugger message. I believe the red font section is causing the issue. Any help would be greatly appreciated, since I am using this formula in multiple columns and sheets throughout the workbook.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("P:P, X:X"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub

Also, is there a way to make the date be alphanumeric like 10-Sep-2018? In advance, thank you.

Best
Novice
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If you simply want a time stamp in column P when entering data in column O, this macro in the worksheet code module should do what you want in the format you requested.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("O:O")) Is Nothing Then Exit Sub
    Target.Offset(0, 1) = Format(Date, "dd-mmm-yyyy")
End Sub
 
Upvote 0
This will also include the time.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("O:O")) Is Nothing Then Exit Sub
    Target.Offset(0, 1) = Format(Now(), "dd/mm/yyyy h:mm AM/PM")
End Sub
 
Upvote 0
Yay! This seems to work...thank you :)

I did want to ask about one thing - with the previous code, when I deleted the item from the previous column, the date would disappear too. How would I be able to add this to the above formula?

Thanks again!!
 
Upvote 0
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("O:O")) Is Nothing Then Exit Sub
    If Target = "" Then
        Target.Offset(0, 1).ClearContents
    Else
        Target.Offset(0, 1) = Format(Now(), "dd/mm/yyyy h:mm AM/PM")
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top