time and date

john tempest

Board Regular
Joined
Nov 20, 2005
Messages
54
i need to put the current time in cell (A1) and the current date in cell (D5) when a value is entered in (A3)
thank you
johyn tempest
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
One way:

A1: =IF($A$3="","",NOW())
D5: =IF($A$3="","",TODAY())

However, using formulas, the date and time displayed in the cells will update whenever the sheet recalculates.

If you want the date/time to be static, you need to use a macro.
Try this, which would go in the module of the sheet you want to use it on:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
If Target.Address = "$A$3" Then
    Range("A1") = Time
    Range("D5") = Date
End If
Application.EnableEvents = True

End Sub
 
Upvote 0
this is the actual code i entered.the date it produced was january 1900 and the time was 00:00:00,please help. thankyou john tempest :confused:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
If Target.Address = "$B$10" Then
Range("A10") = Time
Range("G10") = Date
End If
Application.EnableEvents = True

End Sub
 
Upvote 0
Not sure, it worked for me with no problems.

You could try formatting the data within the code:
Code:
If Target.Address = "$B$10" Then
    Range("A10").Value = Format(Time, "h:mm")
    Range("G10").Value = Format(Date, "mm/dd/yy")
End If

But maybe it could also be due to the format of the cells themselves; check what they're formatted as.
 
Upvote 0
i got your first code to work. i then tried to expand the range but with no success. the code i used is as follows. please bear with me i am new to all this

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
If Target.Address = "$A$10":"$A$40" Then
Range("A10:A40") = Time
Range("G10:G40") = Date
End If
Application.EnableEvents = True

End Sub

what have i done wrong now
thankyou, john tempest
 
Upvote 0
Ah, ok. I think I see what you're trying to do.

You have to be a bit more specific with the code in this case.

Are you looking for something like this?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False

'if the changed cell was within the range B10:B40
If Not Intersect(Target, Range("B10:B40")) Is Nothing Then

    'insert time in column A of the same row
    Cells(Target.Row, "A").Value = Time
    
    'insert date in column G of the same row
    Cells(Target.Row, "G").Value = Date
End If

Application.EnableEvents = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,207,168
Messages
6,076,907
Members
446,239
Latest member
Home Nest

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