Excel 如何从真实日期中提取时间?

Excel 如何从真实日期中提取时间?,excel,vba,Excel,Vba,我想去掉真实日期的时间戳 2015年11月31日12:00:00至2015年11月31日 我使用以下代码而不使用文本到列(远离它): 要执行的运行时很慢,因为我有将近4000行。是否有其他方法,不使用TexttoColumns?将所有原始数据捕获到变量数组中,并在内存中执行时间截断。调整数组元素后,将新值转储回工作表并设置 坚持使用Int(…)函数转换。CInt和CLng都有舍入趋势,如果时间在12:00之后,可能会提前一个日期。使用数组会更快: Sub strpTime() Dim rg

我想去掉真实日期的时间戳

2015年11月31日12:00:00至2015年11月31日

我使用以下代码而不使用文本到列(远离它):

要执行的运行时很慢,因为我有将近4000行。是否有其他方法,不使用TexttoColumns?

将所有原始数据捕获到变量数组中,并在内存中执行时间截断。调整数组元素后,将新值转储回工作表并设置


坚持使用
Int(…)
函数转换。
CInt
CLng
都有舍入趋势,如果时间在12:00之后,可能会提前一个日期。

使用数组会更快:

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
Sub strpTime()
尺寸rg作为范围,数据()
'加载数组中的值
设置rg=Range(“B2”).Resize(ActiveSheet.UsedRange.Rows.Count)
数据=rg.value
'如果单元格不为空,则删除时间
对于i=1到UBound(数据)
如果数据(i,1)为空,则数据(i,1)=数据(i,1)或0
接下来我
'将值复制回工作表
rg.value=数据
rg.NumberFormat=“dd/mm/yy”
端接头

VBA有什么问题?(顺便说一句,请不要使用模棱两可的DMY/MDY示例)我有很多列,并且在其他列上已经有了宏。因此,如果我使用TexttoColums,我必须返回我的宏,并相应地在我拥有的每一列上移动该列。PS:抱歉,日期示例不明确。我没有意识到这一点。我觉得你的代码很好,在4000行上应该很快(对我来说不到一秒钟)。为什么不能在工作表中直接使用舍入?这将剥夺时间的一部分。这两个版本的代码与@Jeeped是了不起的!谢谢你的这些,我会两个都试试,这样更好。非常感谢您的投入。干杯
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
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