2016-06-30 140 views
0

使用Microsoft Excel 2010; Visual Basic for Applications(VBA)運行時錯誤'1004':對象'工作簿'的方法'打開'失敗

嘗試編寫過程以打開工作簿並將電子表格從一個工作簿複製到活動工作簿。

運行時error'1004' :方法‘工作簿‘’對象’打開失敗

下面是代碼我使用:

'Declares variables 
Dim ToBook As Workbook 
Dim FromBook As String 
Dim FromSheet As Worksheet 
Dim diaTitle As String 
Dim FilterName As String 
'----------------------------------------------------------------------------------------------- 
Set ToBook = ActiveWorkbook 
diaTitle = "Select Systems List" 
FromBook = Application.GetOpenFilename(_ 
    FileFilter:=FilterName, _ 
    FilterIndex:=2, _ 
    Title:=diaTitle) 
If FromBook = "False" Then 
    Exit Sub 
End If 
Workbooks.Open _ 
    Filename:=FromBook, _ 
    UpdateLinks:=xlUpdateLinksNever, _ 
    ReadOnly:=False, _ 
    Format:=5, _ 
    Password:="", _ 
    WriteResPassword:="", _ 
    IgnoreReadOnlyRecommended:="", _ 
    Origin:="", _ 
    Delimiter:="", _ 
    Editable:="", _ 
    Notify:="", _ 
    Converter:="", _ 
    AddToMru:="", _ 
    Local:="", _ 
    CorruptLoad:=xlNormalLoad 
Set FromSheet = Workbooks(FromBook).Worksheets("Sheet1") 
'----------------------------------------------------------------------------------------------- 
FromSheet.Copy _ 
    After:=ToBook.Worksheets(6) 
Workbooks(FromBook).Close _ 
    SaveChanges:=False, _ 
    Filename:=FromBook, _ 
    RouteWorkbook:="" 
+0

什麼線路上的錯誤? – KyloRen

+0

Workbook上發生錯誤。打開行 – TroyPilewski

+0

什麼是FromBook在開放行? –

回答

0

User3598756是正確的,你需要使用一個Variant類型。我還簡化了一下你的代碼,並打開&設置在if statement工作簿,以及增加了對用戶msgbox

'Declares variables 
Dim ToBook As Workbook: Set ToBook = ActiveWorkbook 
Dim FromSheet As Worksheet 
Dim FromWB As Workbook 

Dim FromBook As Variant 
Dim diaTitle As String 
Dim FilterName As String 

'----------------------------------------------------------------------------------------------- 
diaTitle = "Select Systems List" 

FromBook = Application.GetOpenFilename(_ 
    FileFilter:=FilterName, _ 
    FilterIndex:=2, _ 
    Title:=diaTitle) 

If FromBook = "False" Then 
    MsgBox "You did not open any file so the macro could not proceed" 
    Exit Sub 
Else 
    Set FromWB = Workbooks.Open(FromBook, xlUpdateLinksNever, False, 5, , , , , , , , , , , xlNormalLoad) 
    Set FromSheet = FromWB.Worksheets("Sheet1") 
End If 

'----------------------------------------------------------------------------------------------- 
FromSheet.Copy _ 
    After:=ToBook.Worksheets(6) 

FromWB.Close savechanges:=False 
0

嘗試下面的代碼,我修改您的Open行,因爲你沒有使用最反正參數...

Dim FromBook As String 
Dim FromSheet As Excel.Worksheet 
Dim diaTitle As String 
Dim FilterName As String 
'----------------------------------------------------------------------------------------------- 
Set ToBook = ActiveWorkbook 
diaTitle = "Select Systems List" 
FromBook = Application.GetOpenFilename(FileFilter:=FilterName, _ 
    FilterIndex:=2, _ 
    Title:=diaTitle) 

If FromBook = "False" Then 
    Exit Sub 
End If 
' minimized your open file parameters, since you are puting blanks anyway 
Workbooks.Open FromBook, xlUpdateLinksNever, False, 5 

Set FromSheet = ActiveWorkbook.Worksheets("Sheet1") 
0

嘗試類似如下

  • 使用Variant類型返回的結果:

    Dim FromBook As Variant 
    
  • 使用一個布爾值,返回的結果比較:

    If FromBook = False Then Exit Sub 
    
相關問題