Excel:每x秒重新计算一次

我的一个电子表格处理涉及(除其他外)当前date和时间的各种计算,它将很好地自动刷新一次,而不是手动按F9或更改其中一个单元格。

Excel中有什么方法可以设置电子表格每隔x秒自动重新计算一次?

我一直无法在Excel本身find一个设置,也许表明没有这样的function存在。 如果没有,这可以用VBA来实现吗? (后者可能听起来可能不是一个愚蠢的问题,但我之前没有编写Excelmacros的经验,因此也不知道它在操作电子表格方面的function。)

此代码将创build一个时钟,每10秒更新一次。
请注意,它只刷新特定的单元格,而不是整个工作簿 – 这意味着您可以将计算选项留在任何满意的位置:

Dim SchedRecalc As Date Sub Recalc() 'Change specific cells Range("A1").Value = Format(Now, "dd-mmm-yy") Range("A2").Value = Format(Time, "hh:mm:ss AM/PM") 'or use the following line if you have a cell you wish to update Range("A3").Calculate Call StartTime ' need to keep calling the timer, as the ontime only runs once End Sub Sub StartTime() SchedRecalc = Now + TimeValue("00:00:10") Application.OnTime SchedRecalc, "Recalc" End Sub Sub EndTime() On Error Resume Next Application.OnTime EarliestTime:=SchedRecalc, _ Procedure:="Recalc", Schedule:=False End Sub 

并确保它停止在本工作簿模块中:

 Private Sub Workbook_BeforeClose(Cancel As Boolean) EndTime End Sub 

Goto Developer Visual basic Editor – 右键单击​​工作簿 – 插入模块(确保您有手动计算

在模块中

 Sub run_over Timetorun = Now + timevalue("00:00:10") application.ontime timetorun,"Refresh_all" End Sub Sub Refresh_all Activeworkbook.Refreshall End Sub Sub auto_close() Application.OnTime timetorun, Refresh_all, , False End Sub 

根据需要更改“00:00:00”格式的时间