1. 程式人生 > >Excel VBA 不開啟Excel檔案訪問其中內容的方法

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