Attached is one potential solution for this as a vbs that can be run from vba
if that is available
Thanks to Sid Rout for his suggested edits to RecursiveFile(objWB)
Caution: It is possible that too many simultaneous books being open (I got to 512 during vbs
recursion hell) will lead to memory issues - in that case each major branch should be updated in turn, then those workbooks closed before proceeding to the next branch.
What it does
- Opens up a workbook held by
strFilePath
- checks whether there are any linked workbooks in 1 , if so opens them (B, B1, B2 etc)
- the code then looks for any links in each of the workbooks from (2), then opens all these in turn (C1 and C2 for B etc)
- each open book name is stored in an array,
Arr
- When all the books are opened, the initial workbook will have been updated, the recursive code ends, and all the open books except
strFilePath
are closed without saving
strFilePath
is then saved and closed
- the code tidies up
EDIT: Updated code to fix the vbs recursion issue
Public objExcel, objWB2, lngCnt, Arr()
Dim strFilePath, vLinks
`credit to Sid Rout for updating `RecursiveFileRecursiveFile(objWB)`
Erase Arr
lngCnt = 0
Set objExcel = CreateObject("Excel.Application")
strFilePath = "C:\temp\main.xlsx"
With objExcel
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
End With
Set objWB = objExcel.Workbooks.Open(strFilePath, False)
Call RecursiveFile(objWB)
For Each vArr In Arr
objExcel.Workbooks(vArr).Close False
Next
objWB.Save
objWB.Close
Set objWB2 = Nothing
With objExcel
.DisplayAlerts = True
.ScreenUpdating = True
.EnableEvents = True
.Quit
End With
Set objExcel = Nothing
MsgBox "Complete"
Sub RecursiveFile(objWB)
If Not IsEmpty(objWB.LinkSources()) Then
For Each vL In objWB.LinkSources()
ReDim Preserve Arr(lngCnt)
'MsgBox "Processing File " & vL
Set objWB2 = objExcel.Workbooks.Open(vL, False)
Arr(lngCnt) = objWB2.Name
lngCnt = lngCnt + 1
RecursiveFile objWB2
Next
End If
End Sub
Working ScreenShots
x
number of "B" files in the same folder or can they be in different folder? – Mizuki