我想在Excel 2007的单元格A1中显示一个时钟。我熟悉NOW()和TODAY(),但它不像我想要的那样每1分钟刷新一次。你知道,就像一个正在运行的时钟。我只想将h:mm中的当前时间放在单元格A1中。这可能吗?
从这个时钟开始,我将做进一步的计算,比如自从我上次做了活动X,Y和Z以来已经有多长时间了。谢谢你。
答案 0 :(得分:7)
找到我在上面评论中提到的code。要测试它,请执行以下操作:
Sheet1
中更改单词A1
的单元格高度和宽度,如下面的快照所示。 Start Timer
按钮,然后点击Assign Macros
。选择StartTimer
宏。End Timer
按钮,然后点击Assign Macros
。选择EndTimer
宏。现在点击启动计时器按钮,您将看到在单元格A1
中更新的时间。要停止更新时间,请单击结束计时器按钮。
代码(已审查和测试)
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 TimerID As Long, TimerSeconds As Single, tim As Boolean
Dim Counter As Long
'~~> Start Timer
Sub StartTimer()
'~~ Set the timer for 1 second
TimerSeconds = 1
TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
End Sub
'~~> End Timer
Sub EndTimer()
On Error Resume Next
KillTimer 0&, TimerID
End Sub
Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
ByVal nIDEvent As Long, ByVal dwTimer As Long)
'~~> Update value in Sheet 1
Sheet1.Range("A1").Value = Time
End Sub
<强>快照强>
答案 1 :(得分:5)
请参阅以下代码(taken from this post)
将此代码放入VBA中的模块(开发人员选项卡 - &gt; Visual Basic)
Dim TimerActive As Boolean
Sub StartTimer()
Start_Timer
End Sub
Private Sub Start_Timer()
TimerActive = True
Application.OnTime Now() + TimeValue("00:01:00"), "Timer"
End Sub
Private Sub Stop_Timer()
TimerActive = False
End Sub
Private Sub Timer()
If TimerActive Then
ActiveSheet.Cells(1, 1).Value = Time
Application.OnTime Now() + TimeValue("00:01:00"), "Timer"
End If
End Sub
您可以在工作簿打开时调用“StartTimer”函数,并通过将以下代码添加到Visual Basic编辑器中的工作簿Visual Basic“This.Workbook”类中,每分钟重复一次。
Private Sub Workbook_Open()
Module1.StartTimer
End Sub
现在,每经过1分钟后,将调用计时器程序,并将单元格A1设置为等于当前时间。