Macro to refresh an entire excel workbook(all data

2019-06-05 00:48发布

I have the following Macro to refresh my workbook. At this point, this is the same as clicking on the refresh all button. Is there a time element that I can add to this code to get it to refresh all data connections and all calculations on all the worksheets in my workbook every 15 minutes.

The workbook has cells pulling data from SharePoint list items and contain typical formula calculations as well.

Sub Workbook_RefreshAll()
ActiveWorkbook.RefreshAll
End Sub

3条回答
欢心
2楼-- · 2019-06-05 00:59

You can use the Application.OnTime method to schedule a macro to be run in the future: https://msdn.microsoft.com/en-us/library/office/ff196165.aspx

Yet, this is a one time event only. To make it recursive, you'll have to include in that macro (yet again) another Application.OnTime to ensure a calling "every 15 minutes".

查看更多
在下西门庆
3楼-- · 2019-06-05 01:00

to refresh all calculations you can use:

application.Calculate

P.S.: Sorry my english

查看更多
迷人小祖宗
4楼-- · 2019-06-05 01:15

Enter the following in a standard module:

Public RunWhen As Double
Public Const cRunIntervalMinutes = 15
Public Const cRunWhat = "Workbook_RefreshAll"

Sub StartTimer()
    RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)
    Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
         schedule:=True
End Sub

Sub StopTimer()
   On Error Resume Next
   Application.OnTime earliesttime:=RunWhen, _
       procedure:=cRunWhat, schedule:=False
End Sub

Sub Workbook_RefreshAll()
    Application.CalculateFullRebuild
    ActiveWorkbook.RefreshAll
    Call StartTimer
End Sub

To begin the process run StartTimer() and to end the process run StopTimer()

Adapted from Chip Pearson's Site

I used some Shapes to run the macros:

enter image description here

查看更多
登录 后发表回答