数囊
发布于 2026-08-06 / 8 阅读
0

excel某单元格显示时钟vba代码

Option Explicit

' Excel 2007 专用API声明,去掉所有高版本关键字
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Dim TimerID As Long

Sub StartClock()
    If TimerID <> 0 Then
        KillTimer 0, TimerID
        TimerID = 0
    End If
    
    TimerID = SetTimer(0, 0, 1000, AddressOf TimerProc)
    'MsgBox "实时时钟已启动", vbInformation
End Sub

Sub StopClock()
    If TimerID <> 0 Then
        KillTimer 0, TimerID
        TimerID = 0
       ' MsgBox "实时时钟已停止", vbInformation
    End If
End Sub

Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    On Error Resume Next
    Sheet1.Range("D2").Value = Now
End Sub