Excel VBA 不打开Excel文件访问其中内容的方法

Public Function GetCellValue(strPath, strFile, strSheet, strA1)
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    If Dir(strPath & strFile) = "" Then     '判断文件是否存在
        '文件不存在时产生运行时错误,此错误会传递给调用此函数的过程
        Err.Raise 12345, "GetCellValue", "No found file"
        Exit Function
    End If
    '调用XLM4.0宏表函数读取指定区域的内容
    '如果指定工作表不存在,返回错误
    GetCellValue = ExecuteExcel4Macro("'" & strPath & "[" & strFile & "]" _
        & strSheet & "'!" & Range(strA1).Address(, , xlR1C1))
End Function

你可能感兴趣的:(转载,技巧备份)