VBA: Open and close all source files to refresh Master Workbook data

cyanidesun

New Member
Joined
Jan 24, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am looking for a way to use VBA in combination with a macro button to force my Master Workbook to open and close all source files in order to refresh all of the external link data I have coming into my workbook. I have about 240 other workbooks that could be potentially pulled from, within 8 different folders (30 in each folder).

I know the real answer is to update values in my links, but I am investigating an issue that is preventing my links from updating (despite everything working when workbooks are open). Therefore I need this workaround. I am using Microsoft 365 for Enterprise.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Something like this should work. You will need to add a sheet called OPEN to the workbook and list the full path and filenames that you want to reopen.
It will then close all Excel Spreadsheets except for the one the script is running in. It will then go through the list in column A on sheet OPEN and open the sheets again.

VBA Code:
Sub CloseAndOpenRefresh()
    Dim currentWorkbook As Workbook
    Dim wsOpenList As Worksheet
    Dim cell As Range
    Dim filePath As String
    
    ' Set reference to the current workbook
    Set currentWorkbook = ThisWorkbook
    
    ' Set reference to the OPEN sheet
    Set wsOpenList = currentWorkbook.Sheets("OPEN")
    
    ' Close all workbooks except the current one
    For Each wb In Application.Workbooks
        If wb.Name <> currentWorkbook.Name Then
            wb.Close SaveChanges:=False
        End If
    Next wb
    
    ' Open workbooks listed with file paths in column A of the OPEN sheet
    For Each cell In wsOpenList.Range("A1:A" & wsOpenList.Cells(wsOpenList.Rows.Count, "A").End(xlUp).Row)
        filePath = cell.Value
        If filePath <> "" Then
            Workbooks.Open filePath
        End 
If
    Next cell
End Sub

t0ny84
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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