Forum Discussion

Tom_Carson's avatar
Tom_Carson
Copper Contributor
Jan 24, 2023
Solved

use if function to enter the time

I have very basic Excel knowledge. Can I use the if function to enter the time in a cell? What I want to do is have a cell show the current time when I enter the date in another cell. Mahalo,
  • OliverScheurich's avatar
    Jan 24, 2023

    Tom_Carson 

     

    =IF(DATE(YEAR(A2),MONTH(A2),DAY(A2))=TODAY(),NOW(),"")

     

    You can try this formula which is in cell C2 in the attached file. You can enter the date (today) in any cell in range A2:A25 and the current time is returned in the adjacent cell in column C. The formula can be filled down to e.g row 1000.

     

     

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rngBereich As Range
    
    Set rngBereich = Range("A2:A25")
    
    If Target.Cells.Count > 1 Then GoTo done
    
    If Not Application.Intersect(Target, rngBereich) Is Nothing Then
    
    If Target.Value = Date Then
    Target.Offset(0, 1).Value = Now
    Else
    Target.Offset(0, 1).Value = ""
    End If
    
    End If
    
    done:
    Application.EnableEvents = True
    Exit Sub
    
    End Sub

     

    An alternative could be this code. In the attached file you can enter the date in a cell in range A2:A25 and the current time is shown in the adjacent cell in column B.

Resources