format date with time VBA

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
If I enter =NOW in a cell and change the format to general it displays a decimal. I would like to be able to select my own date, but include the current time. How can I do this in VBA

i.e. I want to enter 02/13/2020 and current time

Currently, I format my date as format(Date, "mm/dd/yy") however, the time stamp is not showing when i change the format to general.
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Doesn't work. the current time is 12:19 pm

1595409565866.png
 
Upvote 0
Format the cell as a number with at least 6 decimal places and post what it returns.

Also post your full code.
 
Upvote 0
I'm using excel 2019.

This is what I've put together. I don't want to use =NOW() because I will choose the date, I just want to the current time to be added to the date I choose.

VBA Code:
ActiveCell = "02/13/2020" & Format(Now, "hh:mm:ss")

View attachment 18719

I'm using Excel 2016. What version are you using?
 
Upvote 0
What does the below give you when you format the cell as mm/dd/yyyy hh:mm?

VBA Code:
=Datevalue("02/13/2020") + Timevalue(Now - Int(Now))
 
Last edited:
Upvote 0
You can use the format in VBA. For example:
VBA Code:
Activecell = Format(Now(),"mm/dd/yy hh:mm")
or
VBA Code:
ActiveCell.NumberFormat = "mm/dd/yy hh:mm"
ActiveCell.Value = Now
So, with your requirement of setting the date, you could use
VBA Code:
ActiveCell.NumberFormat = "mm/dd/yy hh:mm"
ActiveCell.Value = DateSerial(2020, 2, 13) + Now - Int(Now)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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