当 Sachin Kohli 发表他的答案时,我研究了这个答案。我的回答类似于他使用 VBA 的解决方案,但更进一步概括了这个概念。
如果将此代码复制到标准模块中:
Public Sub RecalculateCells(Optional ByVal CellAdress As String = "", _
Optional ByVal CellWorkbook As String = "", _
Optional ByVal CellWorksheet As String = "", _
Optional ByVal updateTimeInterval As Double = 1#, _
Optional ByVal updateState As Long = 0)
Static continue As Boolean
If updateState = 1 Then continue = True
If updateState = -1 Then continue = False
If continue Then
Workbooks(CellWorkbook).Worksheets(CellWorksheet).Range(CellAdress).Calculate
Application.OnTime Now() + updateTimeInterval / 86400#, _
"'RecalculateCells """ & CellAdress & """, """ & CellWorkbook & _
""", """ & CellWorksheet & """, " & updateTimeInterval & ", 0'"
End If
End Sub
Public Sub StartUpdatingRange(rng As Range, updateTimeInterval As Double)
RecalculateCells "B4", rng.Worksheet.Parent.Name, rng.Worksheet.Name, _
updateTimeInterval, 1
End Sub
Public Sub StopUpdatingRange()
RecalculateCells , , -1
End Sub
您可以在任何给定的时间间隔重新计算任何范围。下面的示例 sub 将每秒重新计算一次单元格 B6 到 B10。
Sub UpdateB6toB10()
StartUpdatingRange ThisWorkbook.Worksheets(1).Range("B6:B10"), 1
End Sub
您可以通过调用StopUpdatingRange 在任何给定时间停止重新计算。您也可以使用不同的范围和不同的更新率多次调用StartUpdatingRange。拨打StopUpdatingRange 将停止所有更新。
请注意,如果计算设置为自动,则所有内容都将以指定的速率更新,而不仅仅是选定的单元格。因此,要实际只更新选定的单元格,请将计算模式设置为手动: