VBA to open, update links, save, and close files.

jaihawk8

Board Regular
Joined
Mar 23, 2018
Messages
58
Office Version
  1. 2016
Platform
  1. Windows
I have 75 earning statements that all reside in the same folder.

When I manually update them, I:

  • Open each file in Excel
  • I receive a message box saying "This workbook contains links to one or more external sources that could be unsafe. If you trust the links, update them to get the latest data. Otherwise, you can keep working with the date you have."
  • I click on the update button
  • The file updates
  • I click save
  • I close the file

I am hoping to find a VBA solution that would open each one of the files automatically and do the steps listed above.

Is this possible?
 
You can add that to part of the loop before the wbSource.Close happens.

For example:
VBA Code:
Do Until sFile = ""
          'open the file
          Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
       
          'update the data links
          ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
         
         'ensure Q2 tab is activated
          wbSource.Sheets("Q2").Activate

          'close the file
          wbSource.Close savechanges:=True
          
          sFile = Dir() 'Added to reset sFile
Loop

If that should change during each quarter change, you could calculate the current quarter. Assuming a January to December fiscal year:

VBA Code:
dim qrt as long: qrt = (Month(date()) + 2) \ 3  'sets the current quarter

Do Until sFile = ""
          'open the file
          Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
       
          'update the data links
          ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
         
         'ensure current Q tab is activated
          wbSource.Sheets("Q" & qrt).Activate 

          'close the file
          wbSource.Close savechanges:=True
          
          sFile = Dir() 'Added to reset sFile
Loop
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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