VBA不打开读取文件内容
子龙奶爸 2010-02-25 01:48:50 我需要用VBA代码不打开文件读取.xls,.xlsx文件。(不打开的原因是:如果已经打开,用open语句会出错)
请问大虾们有什么办法做。我网上查了就是用ExecuteExcel4Macro做。但是我不是很清楚。
里面的sheet1是名字,但是我要是用worksheets(1)(指定编号而不是名字)就出错。而且这个函数执行到
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
这句时就出错,错在Range上面。而且我可能会读这个sheet的大量数据,会不会很慢?或者有什么更好的方法?
分不够再加。
GetValue函数,需要以下四个变量
path: 未打开的Excel文件的路径 (e.g., "d:\test")
file: 文件名(e.g., "test.xls")
sheet: 工作表的名称 (e.g., "Sheet1")
ref: 引用的单元格 (e.g., "C4")
'*********函数如下
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"
Exit Function
End If
' 创建变量
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' 执行XLM 宏
GetValue = ExecuteExcel4Macro(arg)
End Function