Forum Discussion

Kathy_Woodgates's avatar
Kathy_Woodgates
Copper Contributor
Oct 29, 2020
Solved

Inserting a date in a cell when another has a value entered

Hi All   I am new to this community, though have used excel for a good few years, and alothough I have developed some skills with formulas, for some reason I cant get my head around the following: ...
  • HansVogelaar's avatar
    Oct 29, 2020

    Kathy_Woodgates 

    The problem with a formula involving TODAY() or NOW() will be updated continually. You need VBA code instead.

     

    Let's say that you want to add a timestamp in column H when the user enters or changes a value in column D, starting in row 2.

    Right-click the sheet tab.

    Select 'View Code' from the context menu.

    Copy the following code into the worksheet module:

     

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rng As Range
        If Not Intersect(Range("D2:D" & Rows.Count), Target) Is Nothing Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            For Each rng In Intersect(Range("D2:D" & Rows.Count), Target)
                If rng.Value = "" Then
                    Range("H" & rng.Row).ClearContents
                Else
                    Range("H" & rng.Row).Value = Now
                End If
            Next rng
            Application.EnableEvents = True
            Application.ScreenUpdating = True
        End If
    End Sub

     

    Close the Visual Basic Editor.

    Save the workbook as a macro-enabled workbook (.xlsm).

    Make sure that you allow macros when you open the workbook.

Resources