How to Refresh multiple workbook in a directory?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
I am using following code to refresh the workbook. However, If I have more than one workbook (for example 10) then how do I optimize my code.
OR
Do I have to repeat code 10 times? Any idea, please.

Code:
Sub refresh()
Application.ScreenUpdating = False
Dim filename1 As String
filename1 = "C:\Users\Cost.xlsx"


If Len(Dir(filename1)) = 0 Then
    MsgBox "Could not find the file " & filename1, vbExclamation, "File Not Found"
    Exit Sub
Else
    Workbooks.Open filename1, ReadOnly:=False, IgnoreReadOnlyRecommended:=True
    Workbooks(ThisWorkbook.Name).RefreshAll
  
    Windows("Cost.xlsx").Activate
    ActiveWorkbook.Save
    ActiveWorkbook.Close
End If
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Repeat the code or set the Data Connections in each file to refresh when opening the file.
 
Upvote 0
Hi SpillerBD,

Thank you for a response. I have made data connection on each file to the SQL database. The file data is only updated after we do Refresh. And there is more than 10 file which all has a data connection, therefore I am trying to optimize the above code using some kind loop.
 
Upvote 0
I cannot say I'm crazy about it but you can work with this.
Update the DirLoc for your purpose, though make that a test folder with copies of your files.

Code:
Sub RefreshWBs()
    Dim CurFile As String, DirLoc As String
    Dim DestWB As Workbook
    Dim ws As Object 'allows for different sheet types
    Dim OrigWB As Workbook
    [COLOR=#0000ff]DirLoc = "H:\MSO\Documents"[/COLOR]
    CurFile = Dir(DirLoc & "\*.xls*")
    
    Application.ScreenUpdating = True
    Application.EnableEvents = False
            
    Do While CurFile <> vbNullString
        Application.DisplayAlerts = False
        
        'Workbooks.Open Filename:=DirLoc & "\" & CurFile
        Set OrigWB = Workbooks.Open(Filename:=DirLoc & "\" & CurFile)
        
        OrigWB.RefreshAll
        Application.DisplayAlerts = True
        OrigWB.Close SaveChanges:=True
        CurFile = Dir
    Loop
 
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.EnableEvents = True
 
      Set DestWB = Nothing
  End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top