Excel: Recalculating every x seconds

前端 未结 2 1533
一向
一向 2020-12-17 03:20

One of my spreadsheets deals with various calculations involving, among other things, the current date and time and it would be nice to have it automatically refresh itself

相关标签:
2条回答
  • 2020-12-17 03:32

    This code will create a clock, updated every 10 seconds.
    Note that it only refreshes specific cells, and not the entire workbook - this means that you can leave the calculation options at whatever you are happy with:

    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
    

    and, to make sure it stops, in the This Workbook module:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    EndTime
    End Sub
    
    0 讨论(0)
  • 2020-12-17 03:44

    Goto Developer Visual basic Editor - Right Click workbook - insert module (make sure you have manual calculation

    in the module

    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
    

    Change the timing in "00:00:00" format as required

    0 讨论(0)
提交回复
热议问题