[英]Excel: Recalculating every x seconds
我的一個電子表格涉及各種計算,其中包括當前的日期和時間,如果讓它自動刷新一次,而不是手動不得不按F9或改變其中一個單元格,這將是很好的。
Excel中是否有某種方法可以將電子表格設置為每x秒自動重新計算一次?
我無法在Excel中找到設置,也許表明不存在此類功能。 如果沒有,可以通過VBA實現嗎? (后者可能聽起來也可能不是一個愚蠢的問題,但我以前沒有編寫Excel宏的經驗,因此不知道它在操作電子表格方面的能力是什么。)
此代碼將創建一個時鍾,每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
轉到開發人員Visual Basic編輯器 - 右鍵單擊工作簿 - 插入模塊(確保您有手動計算
在模塊中
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”格式的時間
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.