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:
That isn't formula, it is VBA


This gives me an error, however, it seems the code I put together seems to resolve the issue..

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


1595415111564.png
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Now that I am home to test....
VBA Code:
ActiveCell = DateValue("02/13/2020") + (Now - Int(Now))
and format the cell as required.

VBA Code:
ActiveCell = "02/13/2020" & Format(Now, "hh:mm:ss")
will put the data in as text and not a real date.
 
Last edited:
Upvote 0
thank you for that info. I was concern what the ramification of my formatting would be.

Thank you for helping.

Now that I am home to test....
VBA Code:
ActiveCell = DateValue("02/13/2020") + (Now - Int(Now))
and format the cell as required.

VBA Code:
ActiveCell = "02/13/2020" & Format(Now, "hh:mm:ss")
will be the data in as text and not a real date.
 
Upvote 0
.....and the code from post #9?

Left that for the OP to respond so I haven't tested, but it should work correctly.

As should
VBA Code:
ActiveCell = DateValue("02/13/2020") + TimeValue(Now)
 
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)

This would work too, but Mark answer was more to what I was looking for. However, I appreciate you help me out with a possible solution :)
 
Upvote 0

Forum statistics

Threads
1,214,670
Messages
6,120,830
Members
448,990
Latest member
rohitsomani

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