vba display milliseconds high precision

'In Excel, to display milliseconds but leave the date/times 
'values as numbers, use Number Formats on the range:

Select Range-->Format Cells-->Number-->Custom-->[hh]:mm:ss.000 

'Or from VBA:
[a1].NumberFormat = "[hh]:mm:ss.000"
  
'---------------------------------------------------------------------

'But if you need to actually change the data, 
'not just how it displays... 
  
'VBA to transform sub-second date/times to text:
 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)
'Displays:  2020-03-30 14:56:33.437



'VBA to transform sub-second text to Excel date/times:
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")
'Displays:  3794741793.437

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source