简体   繁体   English

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

[英]Run-time error'1004': Method 'Open' of object 'Workbooks' failed

Using Microsoft Excel 2010; 使用Microsoft Excel 2010; Visual Basic for Applications (VBA) Visual Basic应用程序(VBA)

Trying to write a procedure to open a workbook and copy the spreadsheet from one workbook into the active workbook. 尝试编写一个过程以打开工作簿并将电子表格从一个工作簿复制到活动工作簿中。

Run-time error'1004': Method 'Open' of object 'Workbooks' failed 运行时错误“ 1004”:对象“工作簿”的方法“打开”失败

Below is the code I am using: 以下是我正在使用的代码:

'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:=""

Try the following code, I modified your Open line, since you are not using most of the parameters anyway... 尝试下面的代码,我修改了您的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")

Try like follows 尝试如下

  • Use a Variant type for the returned result: 对返回结果使用Variant类型:

     Dim FromBook As Variant 
  • use a boolean value to compare the returned result to: 使用布尔值将返回的结果与:

     If FromBook = False Then Exit Sub 

User3598756 is right, you need to use a Variant type. User3598756是正确的,您需要使用Variant类型。 I also simplified a bit your code and open & set the workbook in the if statement as well as adding a msgbox for users: 我还简化了您的代码,并在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误“1004”-对象“工作簿”的方法“打开”失败 - Run-time Error '1004' - Method 'Open' of object 'Workbooks' failed 对象“工作簿”的运行时错误1004方法“添加”失败 - Run time error 1004 method 'add' of object 'workbooks' failed 运行时错误'1004'对象'_Global'的方法'Range'失败 - Run-time error '1004' Method 'Range' of object'_Global' failed 运行时错误'1004':对象'_Global'的方法'Range'失败6 - Run-time error '1004' : Method 'Range' of object'_Global' failed 6 运行时错误'1004' - 对象'_Global'的方法'范围'失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed 运行时错误“ 1004”:对象“ _Global”的方法“相交”失败 - Run-time error '1004': Method 'Intersect' of object' _Global' failed 运行时错误'1004':对象'_Worksheet'的方法'OLEObjects'失败 - Run-time error '1004': Method 'OLEObjects' of object'_Worksheet' failed 运行时错误“1004”对象“_Global”的方法“行”失败 - Run-time error '1004' Method 'Rows' of object '_Global' Failed 运行时错误“1004”-object“_Global”的方法“范围”失败 - Run-time error '1004' - Method 'Range' of object '_Global' failed 运行时错误'1004':对象'_Global'的方法'Range'失败 - Run-time error '1004' : Method 'Range' of object'_Global' failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM