VBA - show clock time with accuracy of less than a second

前端 未结 5 1944
不知归路
不知归路 2020-12-11 08:59

Is there a way to use VBA (excel) to generate a clock time with accuracy to a tenth of a second or less?

eg:

Sub test()
    MsgBox Format(Time, \"hh:         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 09:33

    You can use the Windows API to get a more accurate time (including milliseconds) as follows.

    Private Type SYSTEMTIME
    
      Year As Integer
      Month As Integer
      DayOfWeek As Integer
      Day As Integer
      Hour As Integer
      Minute As Integer
      Second As Integer
      Milliseconds As Integer
    
    End Type
    
    Public Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
    
    Public Function GetMilliseconds()
    '' This function returns an accurate version of the milliseconds elememt of the current date/time
      Dim tSystem As SYSTEMTIME
    
      GetSystemTime tSystem
      GetMilliseconds = tSystem.Milliseconds
    
    End Function
    

    Credit goes to http://custom-designed-databases.com/wordpress/2011/get-milliseconds-or-seconds-from-system-time-with-vba/ where there is also more detailed information on getting the milliseconds from the system time in VBA.

提交回复
热议问题