简体   繁体   中英

VBA - How to remove string of characters in a range cell

This is the value of my date Mar 30 2016 4:46:34:256PM i want to remove the Time to make it like this Mar 30 2016 .

I've tried various ways to format this if value with the format like mm-dd-yyyy , mm-dd-yyyy , yyyy-mm-dd etc . it's working but if tried this Mar 30 2016 4:46:34:256PM as value it's not working. can someone please tell me why?

I tried a simple code to test all the formats but it's not working with this value Mar 30 2016 4:46:34:256PM so i decided to remove the 4:46:34:256PM and that's how i got stock....

Sub formateDate()
Dim lastrow As Long

lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow

Cells(i, 2).NumberFormat = ("dd-mm-yyyy")
Next i
End Sub

from your example you seem to have two consecutive spaces after the year number

should it always be so you could go like follows

Cells(i, 2) = Left(Cells(i, 2), InStr(Cells(i, 2), "  ") - 1)

and then you can also assign it date formats since IsDate WorksheetFunction returns True if called on the resulting values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM