Insert Current Date & Time in a Single Cell

animas

Active Member
Joined
Sep 28, 2009
Messages
396
I need a macro/vba code which will insert current date and time inside a single cell as the following format:
dd/mm/yyyy hh:mm AM/PM (i.e. 01/10/2010 05:34:00 PM)

How to do that?

Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Do you need it to change when the sheet is recalculated or do you need it to be fixed to the date/time it was entered?

If the latter: Ctrl-; {space} Ctrl-Shift-:

Or do you want actual VBA code?
 
Last edited:
Upvote 0
Code:
Format(Now(), "dd/mm/yyyy hh:nn AM/PM")
or:-
Code:
Format(Now(), "dd/mm/yyyy hh:nn:ss AM/PM")
 
Upvote 0
How about for when you want to combine a date and time that is not given by the formula Now()? Can you do something like
Code:
Format(myDate & myTime, "dd/mm/yyy hh:mm:ss AM/PM")
?
 
Upvote 0
How about for when you want to combine a date and time that is not given by the formula Now()? Can you do something like
Code:
Format(myDate & myTime, "dd/mm/yyy hh:mm:ss AM/PM")
?

If myDate and myTime are Dimmed as Date, then you'd probably add them using + rather than concatenate them with &.

The Format function must have a date type variable fed to it as the first argument.
 
Upvote 0
I need a macro/vba code which will insert current date and time inside a single cell as the following format:
dd/mm/yyyy hh:mm AM/PM (i.e. 01/10/2010 05:34:00 PM)

How to do that?

Thanks.

Drop this into your macro where "A1" is the cell where you want to display the date/time stamp.

With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
 
Upvote 0
I have changed the way in which the date and time is written to the worksheet. This is sample data to be used in a generic data extraction and
normalisation userform project.

I originally created the data and applied it to each cell in turn in one line which had two random number generations in it for the date offset and time of day.

This line also formatted the data.

I have now split it into four lines to generate the date, time, write the data to each cell and then format the data.

I'm not normally one for writing code taking up as few lines as possible and would rather keep it simple and easier to understand.

Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,225,888
Messages
6,187,669
Members
453,434
Latest member
fattyhuman

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