✔ 最佳答案
確認路徑或檔明是否正確,錯誤是找不到檔案
可以這句測試看看是否錯誤
MsgBox IIf(Dir("D:\\dept.xls") = "", "檔案不存在", Dir("D:\\dept.xls"))
2012-02-13 17:53:22 補充:
如果執行 MsgBox IIf(Dir("D:\\dept.xls") = "", "檔案不存在", Dir("D:\\dept.xls"))
回傳"檔案不存在",那可以肯定你的檔名或路徑打錯了,再檢查看看
2012-02-13 18:12:38 補充:
如果只是要某幾個儲存格的值,可以用excel 4函數
Sub test()
'複製 C:\test.xls 中的 Sheet1 工作表A1儲存格
Range("A1") = GetValue("C:\", "test.xls", "Sheet1", "A1")
End Sub
Private Function GetValue(path, file, sheet, range_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(range_ref).Range("A1").Address(, , xlR1C1)
GetValue = ExecuteExcel4Macro(arg)
End Function
PS:如果是連格式都要複製或要複製的資料很多,那就一定要開啟了