打印本文 打印本文 关闭窗口 关闭窗口
利用VBA从已关闭的工作薄中取得某一工作表某一单元格数值
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1869  更新时间:2009/6/9 2:18:25  文章录入:mintao  责任编辑:mintao

网上别人写的共享代码如下:

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

打印本文 打印本文 关闭窗口 关闭窗口