Extract only Date From Date & Time

abi_learn_vba

Board Regular
Joined
Nov 6, 2009
Messages
215
Hi,

I have an Cell with value as

"4/13/2011 9:45:01 AM".

As you see it contains date and time. I need to get only date from the cell using a VBA code, like assigning the date to a variable. I know we can do this using INT() function in excel, not sure how this can be used in VBA.

Can any one help me.

-Abi
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try like this

Code:
Sub test()
Dim X As Double, D As Date
X = Range("A1").Value
D = CLng(X)
Range("A2").Value = D
End Sub
 
Upvote 0
Another way [assuming date/time is in cell A1]:

Range("A2").Value = Split(Range("A1").Value," ")(0)
 
Upvote 0
Thanks for your responses..

VoG, your suggestion works fine if the time is in AM, if it is in PM it takes the next date.

For eg. "4/12/2011 12:30:00 PM" with this value as input i get 4/13/2011 as output.

Though doofusboy suggestion works fine, i just wanted to know why the output is varies.

Thanks :).........
 
Upvote 0
Maybe i'm missing something, but VBA has an int function as well.

Just use something like Variable = Int(Range("A1"))
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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