Running Timer

You can use the NOW function to get the time, but what if you needed a running timer? A timer displaying the exact time as the seconds tick by? The following API declarations work together to provide that functionality. The timer is placed in cell A1 of Sheet1:

Public Declare Function SetTimer Lib "user32" _ (ByVal hWnd As Long, ByVal nIDEvent As Long, _ ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Public Declare Function KillTimer Lib "user32" _

(ByVal hWnd As Long, ByVal nIDEvent As Long) As Long Public Declare Function FindWindow Lib "user32" _

Alias "FindWindowA" (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long

Private lngTimerlD As Long Public datStartingTime As Date

Public Sub StartTimer()

lngTimerlD = SetTimer(0, 1, 10, AddressOf RunTimer) End Sub

Public Sub StopTimer()

Dim lRet As Long lRet = KillTimer(0, lngTimerlD)

End Sub

Private Sub RunTimer(ByVal hWnd As Long, _

ByVal uintl As Long, ByVal nEventId As Long, _ ByVal dwParam As Long) On Error Resume Next

Sheet1.Range("A1").Value = Now - datStartingTime End Sub

Run the StartTimer macro to have the current date and time constantly updated in cell A1. In Figure 22.7, the cell has been formatted to show hours, minutes, and seconds.

Figure 22.7

A running timer can be added to the application to show the current time.

Figure 22.7

A running timer can be added to the application to show the current time.

0 0

Post a comment