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
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