网上别人写的共享代码如下:
Private Function GetValue(path, file, sheet, ref) ' 从关闭的工作薄返回值 Dim arg As String '确定文件是否存在 If Right(path, 1) <> "\" Then path = path & "\" If Dir(path & file) = "" Then GetValue = "File Not Found" Exit Function End If '创建公式 arg = "'" & path & "[" & file & "]" & sheet & "'!" & Range(ref).Range("A1").Address(, , xlR1C1) '执行EXCEL4宏函数 GetValue = Application.ExecuteExcel4Macro(arg) End Function
'参数说明 '----------------------------------------------------------------- 'path:文件路径 'file:文件名称 'sheet:工作表名称 'ref: 单元格区域 '----------------------------------------------------------------- Sub kk() Range("a1").Value = GetValue("D:\", "book2.xls", "Sheet1", "b2") End Sub
直接读取c:\test.xls(不打开)中sheet1.a1的值另一种参考代码 (比一楼的写法简单许多) Sub TestGetValue() str0 = "'C:\[test.xls]Sheet1'!R1C1" MsgBox ExecuteExcel4Macro(str0) End Sub
|