我遇到了同样的问题,想从具有易失工作表地址的多个工作簿中提取数据。间接和连接有效,但仅在源文件打开时有效。没有可用于关闭文件的功能。
一个宏是我在这里找到的解决方案;
https://www.mrexcel.com/board/threads/vba-to-pull-data-from-dynamically-named-closed-workbooks.984156/
我的解决方案使用带有可变日期组件(日期是源文件名中的唯一变量)和可变工作表组件(我需要为一个人获取数据,其中每个人的名字都是名字)的源文件名组件列表每个文件中的工作表)。
该列表如下所示;List of File Names
I 和 K(名称)是易变的。通过让这些单元格引用单独的输入单元格以简化查询,这些在我的列表中进行了修改。输入您要查询的姓名和最新的工资单日期。它回顾一组周期。
获取我想要的数据的公式是=COUNTIF(INDIRECT(CONCATENATE(H4,I4,J4,K4,L4)),"S")
S 在每个文件的选项卡 Moe 中出现在D63:U63 范围内多少次。
宏解决方案使用 I 列中的日期信息来打开所有源文件,如下所示(我添加了 cmets 以希望更清楚每个步骤的作用);
Public Sub Macro_Name()
' Opens Timesheets files to allow "Indirect" formula to obtain data
Dim r As Long
' I don't know what this does, but it is necessary
Application.ScreenUpdating = False
' Stops screen updating to speed up operation
Calculate
' Calculates to clear any previous data in the file. File is set to manual calculation.
With ThisWorkbook.ActiveSheet
For r = 4 To .Cells(Rows.Count, "I").End(xlUp).Row
' Starting at row 4, uses data in column I in the command below until the end of the data. You can't have any other data in column I.
Workbooks.Open Filename:="S:\Payroll\Weekly Timesheets " & .Cells(r, "I") & ".xlsm"
' Opens each file called Weekly Timesheets [date].xlsm
Next
Windows("[The name of your working file].xlsm").Activate
Calculate
Range("B2").Select
' This calculates your working file and goes to a convenient "home" cell
End With
Dim wb As Workbook
For Each wb In Application.Workbooks
If Not wb Is ThisWorkbook Then
wb.Close SaveChanges:=False
End If
Next
' This sub routine closes all open Excel files EXCEPT your working file. Best not to have other files open as it is set NOT to save.
Application.ScreenUpdating = True
End Sub
我找到了这个页面来设置手动计算的文件,这是我的首选。
https://excel.tips.net/T001988_Forcing_Manual_Calculation_For_a_Workbook.html
我不确定这个解决方案是否有效,它可能不像我想要的那样优雅,但它对我有用。我希望它有所帮助。
向提供上述核心代码的 MrExcel.com 上的 John_w 大喊大叫。我只是摆弄了一些东西,所以它对我有用。