VBA - Modify Cell Date to remove Time Value

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Hi guys,

I have some dates in the format:

dd/mm/yy hh:mm

with some other dates in the format:

dd/mm/yy.

The ones in the format dd/mm/yy hh:mm are severly messing up my vlookups (despite leaving the last argument absent, or as false). Ideally, when the data is imported I would like my VBA to remove the time stamp completely, leaving the date just as dd/mm/yy.

Does anyone know how to accomplish this?

Thanks all
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Absent is not the same as FALSE in VLOOKUP, by the way.

If they are actual date values, then using Int on them will remove the time part.
 
Upvote 0
Yeah I know Rory, I just couldn't remember which did what, I know that one should effectively ignore the hh:mm bit. I tried them both, and neither got my vlookups working.

How would I use int? Never used it in VBA before.

Thanks for the reply
 
Last edited:
Upvote 0
Try like this

Code:
Sub ToDate()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("A" & i)
        .NumberFormat = "dd/mm/yy"
        .Value = CLng(.Value)
    End With
Next i
End Sub
 
Upvote 0
Thanks Peter,

I am getting a type mismatch on .Value = CLng(.Value)

int works, just not sure how to incorporate it into my VBA code
 
Upvote 0
The cells are formated as dd/mm/yyyy, but the actual cell value for example would be:

01/01/2011 07:00
 
Upvote 0
Make sure here aren't any headings in the range, for example

Rich (BB code):
Sub ToDate()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("A" & i)
        .NumberFormat = "dd/mm/yy"
        .Value = CLng(.Value)
    End With
Next i
End Sub
 
Upvote 0
If the display doesn't match the formatting, then it's text, so you need DateValue:
Code:
.Value = DateValue(.Value)
 
Upvote 0
Appologies, it was because my first row of data was a title not a date.

The above seems to work well, all be it very slowly. Not sure why, maybe is just my computer.

Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,706
Members
452,939
Latest member
WCrawford

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