我想尝试从已关闭的工作簿/工作表中的单个单元格中获取数据,但文件名依赖于打开的工作簿/工作表中的单元格。 这是我开始使用的代码,但无法使其正常工作。
Private Function GetValue(path, file, sheet, ref)
path = "Z:\Jobs\"
file = "4817.xlsx"
sheet = "RTR1"
ref = "A13"
' 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
Sub TestGetValue()
path = "Z:\Jobs\"
file = "Macine Planning.xlsx"
sheet = "Data"
Application.ScreenUpdating = False
For r = 1 To 30
For C = 1 To 18
a = Cells(r, C).Address
Cells(r, C) = GetValue(path, file, sheet, a)
Next C
Next r
Application.ScreenUpdating = True
End Sub
感谢您的帮助。