我想从Cell(1, 1)
中的时间戳中减去任何秒数,其类型为Double
。
这是我想象的功能:
Function subtractSeconds()
Dim timeStamp As Double
Dim timeStampSeconds As Double
timeStamp = Cells(1, 1)
'timeStampSeconds = getSeconds(timeStamp)
'Cells(1, 1) = timeStamp - timeStampSeconds
End Function
在comments的帮助下,到目前为止我所拥有的是什么。我当然是在寻找更优雅的东西:
Function subtractSeconds()
Dim timeStamp As Double
Dim timeStampHours As Long
Dim timeStampMinutes As Long
Dim timeStampSeconds As Long
timeStamp = Cells(1, 1)
Cells(1, 1) = Format(Cells(1, 1), "yyyy/m/d hh:mm:ss")
timeStampHours = DatePart("h", Cells(1, 1))
timeStampMinutes = DatePart("m", Cells(1, 1))
'timeStampSeconds = DatePart("s", Cells(1, 1))
'something like the following to put the date time back together without seconds
'Cells(1, 1) = Format(Cells(1, 1), "yyyy/m/d") & TimeValue(timeStampHours & ":" & timeStampMinutes & ":" & 0))
'Convert back to double
End Function
答案 0 :(得分:2)
如果您能Format()
Cells(1, 1)
,那么它包含的有效VBA日期恰好被格式化为演示数字。
此时你需要
Cells(1, 1).Value = DateAdd("s", -DatePart("s", Cells(1, 1).Value), Cells(1, 1).Value)
然而,这会将单元格中数字的外观更改为日期,即使它仍将存储为数字。如果您想保持号码外观,请使用Value2
:
Cells(1, 1).Value2 = DateAdd("s", -DatePart("s", Cells(1, 1).Value2), Cells(1, 1).Value2)