简体   繁体   中英

Delete time from Date&Time Data in Excel VBA

image_1

As shown in image_1, I have a column of raw date data that comes with time. I want to get rid of it so that it only shows date (as shown in image_2)

image_2 .

I can simply achieve this with the code columns("A").numberformat="mm/dd/yyyy" . However, I found this doesn't really get rid of the time but instead simply hide it. I'm looking for some VBA code that not only visually but in fact removes the time. The expected result should just be like image_2, but when I click into any of the cells and in the formula bar, there's no time shown either.

Appreciate any thoughts on this.

One option using INT :

Sheet1.Range("A2:A19").Value = Sheet1.Evaluate("=INT(A2:A19)")

Or a loop:

Sub Test()
    Dim i As Long
    For i = 2 To 19
        With Sheet1.Range("A" & i)
            If IsDate(.Value) Then
                .Value = Int(.Value)
                .NumberFormat = "mm/dd/yyyy"
            End If
        End With
    Next
End Sub

EDIT : Probably worth finding the last row:

With Sheet1
    Dim lastRow As Long
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row

    .Range("A2:A" & lastRow).Value = .Evaluate("=INT(A2:A" & lastRow & ")")
    .Range("A2:A" & lastRow).NumberFormat = "mm/dd/yyyy"
End With

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