vba now milliseconds code example
Example 1: vba display milliseconds high precision
Select Range-->Format Cells-->Number-->Custom-->[hh]:mm:ss.000
[a1].NumberFormat = "[hh]:mm:ss.000"
Public Function FormatDatePrecision$(d#)
On Error Resume Next
FormatDatePrecision = Format(d / 86400, "yyyy-mm-dd HH:mm:ss")
FormatDatePrecision = FormatDatePrecision & "." & Split(d, ".")(1)
End Function
MsgBox FormatDatePrecision(3794741793.437)
Public Function CDatePrecision#(d$)
On Error Resume Next
CDatePrecision = CDbl(CDate(Split(d, ".")(0)) * 86400)
CDatePrecision = CDatePrecision + CCur(Split(d, ".")(1) / 1000)
End Function
MsgBox CDatePrecision("2020-03-30 14:56:33.437")
Example 2: vba timestamp -- milliseconds
Public Function TimeInMS() As String
TimeInMS = Strings.Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." _
& Strings.Right(Strings.Format(Timer, "#0.00"), 2)
End Function