自动从外部工作簿更新值

我有以下工作簿设置:

在这里输入图像说明

工作簿A有一个x工作簿B的链接,并从中获取数据。 工作簿B有一些其他工作簿的链接并从中获取数据。

练习册A是所有其他练习册包含的一种“概要”。 现在,我必须打开所有的工作簿B,在打开工作簿A之前刷新它们并保存。如果不这样做,工作簿B将不会更新工作簿中的数据C.

是否有可能使用.bat或vbs脚本更新所有的工作簿B? 或者有可能从工作簿A内更新它们?

我可能会补充说,我在这台电脑上使用Excel启动器,所以更好的解决scheme将是兼容的。

附加是一个可能的解决scheme,作为一个可以从vba运行的vbs ,如果可以的话

感谢Sid Rout对RecursiveFile(objWB)build议编辑,

警告:可能太多同时打开的书(在vbsrecursion过程中我得到了512)会导致内存问题 – 在这种情况下,每个主分支应该依次更新,然后在进入下一个分支之前closures这些工作簿。

它能做什么

  1. 打开由strFilePath保存的工作簿
  2. 检查是否有任何链接的工作簿在1,如果这样打开他们(B,B1,B2等)
  3. 然后代码从(2)​​中查找每个工作簿中的任何链接,然后依次打开所有这些链接(B等的C1和C2)
  4. 每个打开的书名都存储在一个数组中, Arr
  5. 当所有书籍都打开时,初始工作簿将被更新,recursion代码结束,并且除strFilePath以外的所有打开的书籍都strFilePath被closures而不保存
  6. 然后strFilePath被保存并closures
  7. 代码整理

编辑:更新代码来解决vbsrecursion问题

 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 

工作ScreenShots

在这里输入图像说明

是的,你可以遍历所有的源B工作簿,在后台打开它们,并将UpdateLinks标志设置为True …

 strFiles=Dir(*path & \.xls*) do workbooks.open strfiles, UpdateLinks:=true workbooks(strfiles).close savechanges:=true strFiles=Dir loop while strfiles<>"" 

这应该给你一个开始

所以,由于VBA不是一种select,我们来试一下VB脚本解决scheme:

 dim objFSO, objExcel, objWorkbook, objFile ' set objExcel= CreateObject("Excel.application") ' objExcel.visible=false objExcel.displayalerts=false ' Set objFSO = CreateObject("Scripting.FileSystemObject") objStartFolder = path ' Set objFolder = objFSO.GetFolder(objStartFolder) ' get collection of files from folder Set colFiles = objFolder.Files ' begin loop through all files returned by Files collection of Folder object For Each objFile in colFiles ' sanity check, is the file an XLS file? if instr(objfile.name,"xls")<>0 then ' could also use right(objfile.name,4)=... Wscript.Echo "Opening '" objFile.Name & "' ..." set objWorkbook=objexcel.workbooks.open objfile.name, updatelinks:=true objexcel.workbooks(objfile.name).close savechanges:=true end if Next ' close Excel objexcel.quit ' kill the instance and release the memory set objExcel=nothing 

尝试一下,看看你如何

这里是VB脚本SDK: MSDN库 – VB脚本