Forum Discussion

Estel333's avatar
Estel333
Copper Contributor
Mar 10, 2023
Solved

Modify VBA code in Excel from dynamic date and time to static

Hi,   1. I am using this VBA code to record dd/mm/yyyy hh:mm in column A whenever a selection is made in column J.   2. Whenever column J is deleted, column A is also deleted which is CORRECT   ...
  • HansVogelaar's avatar
    Mar 10, 2023

    Estel333 

    The following will work if the user edits one cell at a time:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim NewVal As Variant
        If Target.CountLarge > 1 Then Exit Sub
        If Not Intersect(Range("J:J"), Target) Is Nothing Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            If Target.Value = "" Then
                Range("A" & Target.Row).ClearContents
            Else
                NewVal = Target.Value
                Application.Undo
                If Target.Value = "" Then
                    Range("A" & Target.Row).Value = Now
                End If
                Target.Value = NewVal
            End If
            Application.EnableEvents = True
            Application.ScreenUpdating = True
        End If
    End Sub

Resources