我想问一下从真正的约会中剥离时间戳的最佳解决方案是什么?从2015年11月31日上午12:00:00到2015年11月31日。我使用下面的代码而不使用Text to Columns(远离它):
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
上面代码的问题是执行的运行时似乎有点慢,因为我有近4,000行。我在想如果没有使用TexttoColumns还有其他方法。很想听听你的意见。谢谢!
答案 0 :(得分:3)
将所有原始Range.Value2 properties抓取到变量数组中,并在内存中执行时间截断。调整数组元素后,将新值转储回工作表并设置Range.NumberFormat property。
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(...)
功能转换。 CInt
和CLng
都有四舍五入的倾向,如果时间是在12:00之后,可能会提前约会。
答案 1 :(得分:3)
使用数组会更快:
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