|
假设要获得D:\Book1.xls文件Sheet1表中的内容:
Private Sub CommandButton1_Click()
p = "D:"
f = "book1.xls"
s = "Sheet1"
For i = 1 To 10
For j = 1 To 9
a = Cells(j, i).Address
Cells(j, i) = GetValue(p, f, s, a)
Next j
Next i
End Sub
Private Function GetValue(path, file, sheet, ref)
' 从未打开的Excel文件中检索数据
Dim arg As String
' 确保该文件存在
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
MsgBox (path & file & " 文件不存在")
End
End If
' 创建变量
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' 执行XLM 宏
GetValue = ExecuteExcel4Macro(arg)
End Function
看附件
附件:Book1.rar
|