vba Excel 错误代码 1004 - 对象工作簿的方法打开失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19931123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:23:28  来源:igfitidea点击:

Excel error code 1004 - method open of object workbooks failed

excelexcel-vbaruntime-errorvba

提问by Michal Pe?eny

I have this code, which works perfectly in another module, but when I copied it over to module on which I'm working now, It throws

我有这段代码,它在另一个模块中完美运行,但是当我将它复制到我现在正在使用的模块时,它抛出

"error 1004 method open of object workbooks failed"

“错误 1004 方法打开对象工作簿失败”

on the line with "Set TlWb"when I either input the file via dialog box or let the program decide for himself and I can't figure out why, because it seems so banal and there are not many options where it can go wrong. I'm pretty sure that the paths I'm providing to the program are correct and everything is DIMmed properly.

上线"Set TlWb"时,我要么通过输入对话框中的文件或让程序自己决定,我想不通为什么,因为它似乎很平庸和没有很多选择的地方可能出错。我很确定我提供给程序的路径是正确的,并且一切DIM正常。

Here is the code:

这是代码:

'opens open prompt for ABII Parallel Import Tracker - Month/Year.xlsx, on cancel opens actual month
MsgBox "Select ABII Parallel Import Tracker - Month/Year.xlsx file to open. Cancel opens actual month"
With Application.FileDialog(msoFileDialogOpen)
    .Show
    If .SelectedItems.Count = 1 Then
        TlWbPath = .SelectedItems(1)
    End If
End With

If TlWbPath <> "" Then
   Set TlWb = Workbooks.Open(TlWbPath)

Else: Set TlWb = Workbooks.Open(traceLogPath & "ABII Parallel Import Tracker - " & Format(Date, "mmm-yyyy") & ".xlsx")
End If

回答by Michal Pe?eny

Well in the end I figured it out. The input file was corrupted, so I opened it manually and resaved it. Corruption was caused by not copying sources for dropdown list on another sheet.

好吧,最后我想通了。输入文件已损坏,所以我手动打开并重新保存。损坏是由于未在另一张工作表上复制下拉列表的源引起的。