最好的方法从真正的date去除时间?

我想问一下,从真正的date中剥离时间戳的最佳解决scheme是什么? 2015年11月31日12:00:00至31/11/2015 我使用下面的代码,而不使用文本到列(远离它):

Sub strpTime() Dim LR As Long, i As Long Application.Calculation = xlCalculationManual LR = Range("B" & Rows.Count).End(xlUp).Row For i = 2 To LR With Range("B" & i) .NumberFormat = "dd/mm/yy" .Value = Int(.Value) End With Next i Application.Calculation = xlCalculationAutomatic End Sub 

我上面的代码的问题是运行时执行似乎有点慢,因为我有近4000行。 我想如果有其他的方式,而不使用TexttoColumns。 很想听听你的意见。 谢谢!

将所有原始的Range.Value2属性抓取到变体数组中,并在内存中执行时间截断。 一旦数组元素已被调整,将新的值转储回工作表并设置Range.NumberFormat属性 。

 Sub strpTime() Dim lr As Long, d As Long, vDATs As Variant Application.Calculation = xlCalculationManual With Worksheets("Sheet1") With .Range(.Cells(2, 2), .Cells(Rows.Count, 2).End(xlUp)) vDATs = .Value2 For d = LBound(vDATs, 1) To UBound(vDATs, 1) vDATs(d, 1) = Int(vDATs(d, 1)) Next d .Value = vDATs .NumberFormat = "dd/mm/yy" End With End With Application.Calculation = xlCalculationAutomatic End Sub 

坚持与Int(...)函数转换。 CIntCLng都有四舍五入的趋势,如果时间是在12:00以后,可能会推迟一个date。

使用数组会更快:

 Sub strpTime() Dim rg As Range, data() ' load the values in an array Set rg = Range("B2").Resize(ActiveSheet.UsedRange.Rows.Count) data = rg.value ' strip the time if the cell is not empty For i = 1 To UBound(data) If data(i, 1) <> Empty Then data(i, 1) = data(i, 1) Or 0 Next i ' copy the values back to the sheet rg.value = data rg.NumberFormat = "dd/mm/yy" End Sub