EXCEL 动态时间代码

你是不是厌倦了时间在EXCEL是静态的样子?想体验下EXCEL单元格中动态的时间吗?那就跟我来实现!此需求主要通过VBA来实现!

打开EXCEL,按ALT+F11打开VBA编辑器:

在thisworkbook中输入如下代码

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Terminate
Set appTime = Nothing
Set objBtn = Nothing
End Sub
Private Sub Workbook_Open()
Init (1000)
End Sub
…………………………………………………………………………………………………………..

新建时间Time模块输入如下代码

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

Private Declare Function KillTimer Lib "user32" (ByVal hWnd As _
                 Long, ByVal nIDEvent As Long) As Long
Dim hTimer
Public objBtn As CommandBarControl

Sub TimerProc(ByVal hWnd&, ByVal Msg&, ByVal idEvent&, ByVal dwTime&)
    DoEvents
    On Error Resume Next
    Range("A1") = Format(Now, "yyyy-m-d hh:mm:ss")
End Sub

Sub Init(Interval&)
    hTimer = SetTimer(0, 0, Interval, AddressOf TimerProc)
End Sub

Sub Terminate()
    Call KillTimer(0, hTimer)
End Sub

       2011/2/25 20:46:23

 

 

保存代码~看看效果吧~时间会在你的单元格中动态运作,秒针会动态行走!呵呵!当然格式随你自己喜好设定,随你DIY~

分享到: