ExcelVBA制作简易电子时钟的方法

本文为《别怕,Excel VBA其实很简单(第3版)》随书问题参考答案

在ThisWorkbook模块中写入下面的过程:

Private Sub Workbook_Open() Call OnTime方法End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean) Application.OnTime StartTime, "更新时间", , FalseEnd Sub

新插入一个模块,在其中写入下面的代码:

Public StartTime As Date

Sub OnTime方法() StartTime = Now() + TimeValue("00:00:01") Application.OnTime StartTime, "更新时间"End Sub

Sub 更新时间() Range("B3") = Format(Now(), "yyyy-mm-dd hh:mm:ss") Call OnTime方法End Sub

保存、关闭,并重新打开工作簿,就能在活动工作表的B3单元格看到简易的电子时钟了。