Automatic refresh from automatic open

normanbox

New Member
Joined
Jul 8, 2015
Messages
46
I have found some VBA code, which works fine, that when I run the code from a workbook, it opens all the workbooks from a designated file. For example, workbook A macro opens workbooks B, C, D, E... and then closes it again. My problem is that each of the workbooks has external data that needs to be refreshed. Even though I have the external data set up to refresh upon being open, which works fine when manually opening the workbook, it doesn't refresh when I run the code. I even found some code to delay the closing of the workbook thinking that would give it time to refresh, but it doesn't. What I need from somebody is the line or two of code necessary so my workbooks will refresh when the code runs. Here is my code:

Rich (BB code):
Sub LoopAllExcelFilesInFolder()


'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: Squarespace - Claim This Domain


Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog


'Optimize Macro Speed
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic


'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)


    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With


'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings


'Target File Extension (must include wildcard "*")
  myExtension = "*.xlsm"


'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)


'Loop through each Excel file in folder
  Do While myFile <> ""
    'Set variable equal to opened workbook
      Set wb = Workbooks.Open(Filename:=myPath & myFile)
      Application.Wait (Now + TimeValue("0:00:10"))
      
    
    'Save and Close Workbook
      wb.Close SaveChanges:=True


    'Get next file name
      myFile = Dir
  Loop


'Message Box when tasks are completed
  MsgBox "Task Complete!"


ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True


End Sub

Thanks in advance!
 
Last edited by a moderator:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,

If I understand you correctly try a couple of ways.

'ActiveWorkbook.RefreshAll' after you have opened the workbook.
with Set wb = Workbooks.Open(Filename:=myPath & myFile)
or
A workbook open event.
This would need to be placed in the VBA project 'ThisWorkbook' of each workbook so that when it opens it refreshes the workbook.


Code:
Private Sub Workbook_Open()
    ThisWorkbook.RefreshAll
End Sub

There may be issues with timing in either of these methods so you may need to do some further 'research'
 
Last edited:
Upvote 0
Thanks for your help. I've tried both methods you suggested and neither worked. I do see "connecting to web" flash on the screen but that's it, it doesn't refresh. Looks like I'll need to keep researching the issue, unless you or somebody else has a suggestion?
 
Upvote 0
I should have mentioned that when I originally put in your code, I did so incorrectly and got a VB error 400. That's all it said, "400". The funny thing, the workbook that it opened refreshed. I just can't get it to refresh without causing an error first.
 
Upvote 0

Forum statistics

Threads
1,215,573
Messages
6,125,608
Members
449,238
Latest member
wcbyers

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