Forum Discussion
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,
=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.
2 Replies
- OliverScheurichGold Contributor
=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.
- Tom_CarsonCopper ContributorThis is amazing.
Mahalo nui loa! (Thank You Very Much)