VBA For Time Stamp

data808

Active Member
Joined
Dec 3, 2010
Messages
353
Office Version
  1. 2019
Platform
  1. Windows
I have some VBA that I am using to trigger a time stamp when I type into certain cells. The problem is that even if the time stamp has already been triggered and the cell is filled in with the time, it will continue to update the time if I keep updating the cells that trigger it. Is there a way to have it so that if the cells already have a time in it, it will stop the updating of the time? Here is the current code I am using:

VBA Code:
' type in columns A-C to do a time stamp in column D. 
    With Target
        If .Count > 1 Then Exit Sub
        If Not Intersect(Range("A:C"), .Cells) Is Nothing Then
        Application.EnableEvents = False
        With Range("D" & Target.Row)
            If Not IsDate(.Value) Then
                .NumberFormat = "HH:MM"
                .Value = Now
            End If
        End With
        Application.EnableEvents = True
        End If
    End With
' same as above but different range and target
    With Target
        If .Count > 1 Then Exit Sub
            If Not Intersect(Range("F:O"), .Cells) Is Nothing Then
        Application.EnableEvents = False
        With Range("E" & Target.Row)
            If Not IsDate(.Value) Then
                .NumberFormat = "HH:MM"
                .Value = Now
            End If
        End With
        Application.EnableEvents = True
        End If
    End With
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Check if the cell IsEmpty or = "" ? If not true, there is a value there already. Not bullet proof since the value can be "dog" so up to you to decide how many tests you need. I can't tell from the code where it is - E or D so I kept this simple. Maybe it's both?
 
Upvote 0
Give this a go
VBA Code:
    ' type in columns A-C to do a time stamp in column D.
    With Target
        If .Count > 1 Then Exit Sub
        If Not Intersect(Range("A:C"), .Cells) Is Nothing Then
        Application.EnableEvents = False
        With Range("D" & Target.Row)
            If Not IsDate(.Value) And .Value = "" Then
                .NumberFormat = "HH:MM"
                .Value = Now
            End If
        End With
        Application.EnableEvents = True
        End If
    End With
' same as above but different range and target
    With Target
        If .Count > 1 Then Exit Sub
            If Not Intersect(Range("F:O"), .Cells) Is Nothing Then
        Application.EnableEvents = False
        With Range("E" & Target.Row)
            If Not IsDate(.Value) And .Value = "" Then
                .NumberFormat = "HH:MM"
                .Value = Now
            End If
        End With
        Application.EnableEvents = True
        End If
    End With
 
Upvote 0
Solution
Give this a go
VBA Code:
    ' type in columns A-C to do a time stamp in column D.
    With Target
        If .Count > 1 Then Exit Sub
        If Not Intersect(Range("A:C"), .Cells) Is Nothing Then
        Application.EnableEvents = False
        With Range("D" & Target.Row)
            If Not IsDate(.Value) And .Value = "" Then
                .NumberFormat = "HH:MM"
                .Value = Now
            End If
        End With
        Application.EnableEvents = True
        End If
    End With
' same as above but different range and target
    With Target
        If .Count > 1 Then Exit Sub
            If Not Intersect(Range("F:O"), .Cells) Is Nothing Then
        Application.EnableEvents = False
        With Range("E" & Target.Row)
            If Not IsDate(.Value) And .Value = "" Then
                .NumberFormat = "HH:MM"
                .Value = Now
            End If
        End With
        Application.EnableEvents = True
        End If
    End With
Thank you very much. I knew it was something easy but when I saw how little of the code you removed, I was not anticipating that. I was trying to add in an IF cell is <> 0 statement. lol

Thanks again for the help.
 
Upvote 0
Check if the cell IsEmpty or = "" ? If not true, there is a value there already. Not bullet proof since the value can be "dog" so up to you to decide how many tests you need. I can't tell from the code where it is - E or D so I kept this simple. Maybe it's both?
Thanks for the help. Looks like @ouvay solved it. Appreciate your suggestion though.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,867
Members
449,053
Latest member
Mesh

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