
VBA
用
VBA从没打开过的
excel里读数据(转) 有时候想从一个没打开的
excel文件里读数据,可以用这个方法,用XLM宏搞。 注意哈,这个函数不能直接当公式用,得写在
VBA里。 函数叫GetValue,需要四个参数: path:文件路径,比如 d:test file:文件名,比如 test.xls sheet:表单名,比如 Sheet1 ref:单元格位置,比如 C4 下面上代码 '函数开始 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 & & sheet & '! & _ Range(ref).Range(A1).Address(, , xlR1C1) '执行XLM宏 GetValue = Execute
excel4Macro(arg) End Function '函数结束 怎么用? 把上面那段扔进
VBA的模块里,然后写个调用的例子,比如读 d:test 下 test.xls 里 Sheet1 的 A1 数据 代码示例: Sub TestGetValue() p = d:test f = test.xls s = Sheet1 a = A1 MsgBox GetValue(p, f, s, a) End Sub 还有一个例子,可以从没打开的文件里一口气读1200个数据(100行12列),然后填到当前表格里。