运行时错误' 1004':方法'打开'对象'工作簿'失败

时间:2016-06-30 21:18:43

标签: vba excel-vba excel-2010 excel

使用Microsoft Excel 2010; Visual Basic for Applications(VBA)

尝试编写一个过程来打开工作簿并将电子表格从一个工作簿复制到活动工作簿中。

运行时错误' 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:=""

3 个答案:

答案 0 :(得分: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")

答案 1 :(得分:0)

尝试以下

  • 对返回的结果使用Variant类型:

    Dim FromBook As Variant
    
  • 使用布尔值将返回的结果与:

    进行比较
    If FromBook = False Then Exit Sub
    

答案 2 :(得分: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