我在Excel工作表中有一个数字列表。这些是返回号,所有这些在我的桌面上都有一个关联的文件。例如,数字625在我的桌面上具有Excel文件RGA#625。大约有800个数字。如何从各自的excel文件中为每个数字提取一个细节?
所需输出 635 372883 625 273232 743 323724
第二列应从单独的excel文件中提取。在这些文件中,该编号的位置将为F10或F11。
答案 0 :(得分:0)
我使用了here
中的以下功能Private 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 an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
然后我做出以下假设:i)返回编号在A列中ii)文件命名约定始终为RGA#625.XLSX iii)数据始终位于F10中的sheet1上
Sub GetData_for_ReturnNumbers()
Const DATA_DIR = "<put your directory here>"
Const COL_RETNR = 1
Const FILE_START = "RGA # "
Const FILE_EXT = ".XLSX"
Const SHEET1 = "Sheet1"
Dim sngCell As Range
Dim filename As String
Dim rg As Range
Set rg = Range("A2:A4")
For Each sngCell In rg
filename = FILE_START & sngCell.Value & FILE_EXT
sngCell.Offset(, 1) = GetValue(DATA_DIR, filename, SHEET1, "F10")
Next
End Sub