这是您尝试做的事情的一个很好的链接。从关闭的文件中获取信息
这是一些帮助您入门的代码。现在它设置为从 sheet2 'A1' 获取第一个文件路径,从 'A2' 获取工作簿名称,并将值返回给 sheet1 'A1'。一旦你完成了这项工作,你将需要遍历你的范围(取决于你如何进行设置)并显示结果。
Sub ReadDataFromAllWorkbooksInFolder()
Dim FolderName As String, wbName As String, r As Long, cValue As Variant
Dim wbList() As String, wbCount As Integer, i As Integer
'Path from sheet two in macro book
FolderName = Worksheets(2).Range("A1").Text
'File Name from sheet two in macro book
Filename = Worksheets(2).Range("A2").Text
wbName = Worksheets(2).Range("A2").Text
cValue = GetValue(FolderName, wbName, "Sheet1", "A1")
Worksheets(1).Cells(1, 1).Formula = cValue
End Sub
Function GetValue(Path, File, Sheet, Ref)
'Retrieves a value from a closed workbook
Dim Arg As String
'Make sure the file exists
If Right(Path, 1) <> "\" Then Path = Path & "\"
If Dir(Path & File) = "" Then
GetValue = "File not Found"
Exit Function
End If
'Create the argument
Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & Range(Ref.Range("A1").Address(, , xlR1C1))
'Execute XLM macro
GetValue = ExecuteExcel4Macro(Arg)
End Function