Skip to content

Bulk refreshing workbooks

Workbooks that pull data through Power Query go stale until someone refreshes them. There are a few ways to refresh them on a schedule:

  1. publish the reports to Power BI
  2. let SharePoint refresh the workbooks
  3. use a desktop tool that opens and refreshes each workbook in turn

Power BI refreshes reports on a schedule, but everyone who views a shared report normally needs a paid licence, which adds up across a team. SharePoint works if the organisation has it set up to refresh workbooks and the files are meant to be shared there, but many people have no access to it, or need to send the refreshed files out in other ways. That leaves the third option, a desktop tool, which this page describes.

The tool is semi-automatic, because someone has to start it. A run has three steps:

  1. Open the refresh tool workbook.
  2. Mark the folders to refresh.
  3. Click the refresh button.

The tool then works through each file in the marked folders in turn, opening it, refreshing it, saving it and closing it. When it finishes, it lists which workbooks were refreshed and how long each one took.

  • Create an empty folder to hold the workbooks. It can be on a shared network drive or on the local PC, beside the refresh tool.
  • Inside it, create a subfolder for each group of workbooks that is refreshed on its own cycle. The example uses refresh frequencies, but any grouping that suits the business will do.

A Windows Explorer window showing an AutoRefresh folder that contains Daily, Monthly and Weekly subfolders

Then move each workbook into the right subfolder.

  • Open the tool and fill in the setup cells so that they point to the main folder. The full path of the folder goes in cell J1, and a file pattern, such as *.xlsx, goes in cell J2 if only some files should be refreshed.

The tool’s setup area, with the start folder set to the AutoRefresh folder and the file type set to xlsx files, the Update File List and Refresh Selected Reports buttons, and a Folders to Refresh table with an x against Daily

  • Click Update File List. The table on the left fills with each subfolder and the files in it, as in the example below. Column C is hidden, and holds each file’s full path.

The file list with columns for Refresh Frequency, Folder Name, File, Size, Last Saved, Status and Time, showing seven workbooks marked Daily and Done

  • Type a frequency, such as Daily, Weekly or Monthly, against each file in the Refresh Frequency column. This is needed only once, when the tool is set up.
  • Files can also be added to the list by hand, as long as their full path is entered in column C.

To refresh the workbooks:

  1. Open the refresh tool.
  2. In the table on the right, put an x against each folder to refresh.
  3. Click Refresh Selected Reports, and leave it to run.

The macros behind the tool’s buttons are listed below.

Sub RefreshAll()
RefreshFiles
End Sub
Sub UpdateFileList()
ListAllFolders
FileLister
End Sub
Sub FileLister()
Dim X As String
Dim myDir As String
Dim Y As Integer
Dim myFileSystem As Object
Dim myFileObject As Object
Dim myFreq As Variant
Dim myPattern As String
Application.ScreenUpdating = False
Set myFileSystem = CreateObject("Scripting.FileSystemObject")
Sheets("files").Activate
Range("B1").CurrentRegion.Offset(1, 0).Clear
Range("B2").Select
myPattern = Range("Pattern").Value
Sheets("folders").Activate
Range("b6").Select
On Error GoTo FolderError
While Selection.Value <> ""
myDir = Selection.Value
myFile = myDir & myPattern
Sheets("files").Activate
X = Dir(myFile, 2)
Do Until X = ""
myFreq = Split(myDir, "\")
Selection.Value = myFreq(UBound(myFreq) - 1)
Selection.Offset(0, 1).Value = myDir
Selection.Offset(0, 2).Value = X
Set myFileObject = myFileSystem.Getfile(myDir & X)
Selection.Offset(0, 3).Value = myFileObject.Size
Selection.Offset(0, 3).NumberFormat = "#,##0"
Selection.Offset(0, 4).Value = myFileObject.DateLastModified
Selection.Offset(1, 0).Select
X = Dir
Loop
noFiles:
Sheets("folders").Activate
Selection.Offset(1, 0).Select
Wend
ExitHere:
On Error GoTo 0
Set myFileSystem = Nothing
Set myFileObject = Nothing
Sheets("files").Activate
Application.ScreenUpdating = True
Exit Sub
FolderError:
Resume noFiles
End Sub
Sub ListAllFolders()
Dim X As Integer
Dim myDir As String
Application.ScreenUpdating = False
myDir = Range("StartFolder").Value
If myDir = "" Then
X = MsgBox("Enter a start folder in cell " & Range("startfolder").Address, vbOKOnly)
GoTo ExitHere:
End If
Sheets("folders").Activate
Range("b6").CurrentRegion.Clear
If Right(myDir, 1) <> "\" Then myDir = myDir & "\"
Range("b6").Select
On Error GoTo Error:
ListFolders (myDir)
GoTo ExitHere:
Error:
X = MsgBox("Invalid Folder Name", vbOKOnly)
ExitHere:
Sheets("files").Activate
Range("StartFolder").Value = myDir
Application.ScreenUpdating = True
End Sub
Sub ListFolders(myDir)
Dim FS, f, f1, fc, s
If myDir = Empty Then GoTo ExitHere:
Set FS = CreateObject("Scripting.FileSystemObject")
Set f = FS.GetFolder(myDir)
Set fc = f.SubFolders
For Each f1 In fc
Selection.Value = myDir & f1.Name & "\"
Selection.Offset(1, 0).Select
Next
ExitHere:
Set FS = Nothing
Set f = Nothing
Set fc = Nothing
End Sub
Sub RefreshFiles()
Dim tbRef As Object
Dim myFile As String
Dim toDo As Collection
Dim myStart As Date
Set tbRef = ActiveSheet.ListObjects("table2")
Application.DisplayAlerts = False
'create to do list
Set toDo = New Collection
Intersect(tbRef.ListRows(1).Range, tbRef.ListColumns(1).Range).Select
While Selection <> ""
If UCase(Selection.Offset(0, 1)) = "X" Then
toDo.Add (Selection.Value)
End If
Selection.Offset(1, 0).Select
Wend
'execute todo list
Range("A2").Select
While Selection <> ""
For Each Item In toDo
If Selection.Value = Item Then
myFile = Selection.Offset(0, 2) & Selection.Offset(0, 3)
myStart = Now()
Workbooks.Open Filename:=myFile
Application.DeferAsyncQueries = True
ActiveWorkbook.RefreshAll
Application.CalculateUntilAsyncQueriesDone
Application.DeferAsyncQueries = False
DoEvents
ActiveWorkbook.Save
ActiveWorkbook.Close
Selection.Offset(0, 5).Value = Now
Selection.Offset(0, 6).Value = "Done"
Selection.Offset(0, 7).Value = Now() - myStart
Selection.Offset(0, 7).NumberFormat = "hh:mm:ss"
Application.Wait Now + TimeValue("00:00:02")
End If
Next Item
Selection.Offset(1, 0).Select
Wend
Set toDo = Nothing
Set tbRef = Nothing
Application.DisplayAlerts = True
End Sub