Adding/Deleting Date

CEG

Board Regular
Joined
Jan 3, 2010
Messages
74
Wondering if someone could help me with this code. What I am attempting to do is add a date into cell K when a number is entered into cell C. But, if cell C is modified and there is already a date in cell K, then do nothing. But, I also want to delete the date in cell K if the number in cell C is deleted. But, the way I have it written, it exits the sub too early, and I can't figure out how to re-write it. Thanks

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Adds date/time in column "K" when P/N is entered in column "C"
    If Not Intersect(Target, Range("C:C")) Is Nothing Then
        On Error Resume Next
        Application.EnableEvents = False
 
        If Target > 1 Then
            [COLOR=red]If Target.Offset(0, 8) > 1 Then Exit Sub[/COLOR]
            With Target.Offset(0, 8)
                .Value = "=NOW()"
                .Value = .Value
            End With
        End If
 
        If Target < 1 Then
        Target.Offset(0, 8).ClearContents
        End If
        Application.EnableEvents = True
    End If
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Maybe like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r           As Range
 
    With Target
        If .Column <> 3 Then Exit Sub
        
        Set r = Cells(.Row, "K")
        On Error Resume Next
        Application.EnableEvents = False
 
        If IsEmpty(.Value) Then
            r.ClearContents
        ElseIf VarType(.Value) = vbDouble And IsEmpty(r.Value) Then
            r.Value = Now
        End If
    End With
    
    Application.EnableEvents = True
End Sub
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Adds date/time in column "K" when P/N is entered in column "C"
    If Not Intersect(Target, Range("C:C")) Is Nothing Then
        If Target = "" Then
            Target.Offset(0, 8).ClearContents
        ElseIf IsEmpty(Target.Offset(0, 8)) Then
            Target.Offset(0, 8).Formula = Format(Now(), "DD/MM/YYYY - HH:mm:SS")
        End If
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,572
Members
452,927
Latest member
whitfieldcraig

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